

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=dark>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/deng-2022/blog_2/img/fluid.png">
  <link rel="icon" href="/deng-2022/blog_2/img/icon/dw.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="Memory">
  <meta name="keywords" content="">
  
    <meta name="description" content="Memory 用户中心开发基本流程">
<meta property="og:type" content="article">
<meta property="og:title" content="Memory 用户中心中心-开发文档">
<meta property="og:url" content="https://atomgit.com/deng-2022/blog_2/2023/03/17/Memory%20%E7%94%A8%E6%88%B7%E4%B8%AD%E5%BF%83%E7%B3%BB%E7%BB%9F-%E5%BC%80%E5%8F%91%E6%96%87%E6%A1%A3/index.html">
<meta property="og:site_name" content="Memory&#39;s blog">
<meta property="og:description" content="Memory 用户中心开发基本流程">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://atomgit.com/deng-2022/blog_2/img/artical/girl4.jpg">
<meta property="article:published_time" content="2023-03-17T02:05:47.000Z">
<meta property="article:modified_time" content="2023-04-21T14:36:44.000Z">
<meta property="article:author" content="Memory">
<meta property="article:tag" content="项目">
<meta property="article:tag" content="开发经验">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://atomgit.com/deng-2022/blog_2/img/artical/girl4.jpg">
  
  
  
  <title>Memory 用户中心中心-开发文档 - Memory&#39;s blog</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/deng-2022/blog_2/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/deng-2022/blog_2/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/deng-2022/blog_2/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"atomgit.com","root":"/deng-2022/blog_2/","version":"1.9.4","typing":{"enable":true,"typeSpeed":100,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":false,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":true,"follow_dnt":true,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/deng-2022/blog_2/local-search.xml"};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/deng-2022/blog_2/js/utils.js" ></script>
  <script  src="/deng-2022/blog_2/js/color-schema.js" ></script>
  

  

  

  

  

  

  

  



  
<meta name="generator" content="Hexo 5.4.2"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 75vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/deng-2022/blog_2/">
      <strong>Memory&#39;s blog</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/deng-2022/blog_2/">
                <i class="iconfont icon-home-fill"></i>
                <span>首页</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/deng-2022/blog_2/archives/">
                <i class="iconfont icon-archive-fill"></i>
                <span>归档</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/deng-2022/blog_2/categories/">
                <i class="iconfont icon-category-fill"></i>
                <span>分类</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/deng-2022/blog_2/tags/">
                <i class="iconfont icon-tags-fill"></i>
                <span>标签</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/deng-2022/blog_2/about/">
                <i class="iconfont icon-user-fill"></i>
                <span>关于</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/deng-2022/blog_2/links/">
                <i class="iconfont icon-link-fill"></i>
                <span>友链</span>
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              <i class="iconfont icon-search"></i>
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">
              <i class="iconfont icon-dark" id="color-toggle-icon"></i>
            </a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/deng-2022/blog_2/img/newBG/girl.jpg') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="Memory 用户中心中心-开发文档"></span>
          
        </div>

        
          
  <div class="mt-3">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-author" aria-hidden="true"></i>
        Memory
      </span>
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2023-03-17 10:05" pubdate>
          2023年3月17日 上午
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          38k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          94 分钟
        
      </span>
    

    
    
      
        <span id="leancloud-page-views-container" class="post-meta" style="display: none">
          <i class="iconfont icon-eye" aria-hidden="true"></i>
          <span id="leancloud-page-views"></span> 次
        </span>
        
      
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar category-bar" style="margin-right: -1rem">
    





<div class="category-list">
  
  
    
    
    
    <div class="category row nomargin-x">
      <a class="category-item 
          list-group-item category-item-action col-10 col-md-11 col-xm-11" title="项目"
        id="heading-31ecc0e65b4cd3e6b12577382bb56d6a" role="tab" data-toggle="collapse" href="#collapse-31ecc0e65b4cd3e6b12577382bb56d6a"
        aria-expanded="true"
      >
        项目
        <span class="list-group-count">(6)</span>
        <i class="iconfont icon-arrowright"></i>
      </a>
      
      <div class="category-collapse collapse show" id="collapse-31ecc0e65b4cd3e6b12577382bb56d6a"
           role="tabpanel" aria-labelledby="heading-31ecc0e65b4cd3e6b12577382bb56d6a">
        
        
          
          
  <div class="category-post-list">
    
    
  </div>

          
  
    
    
    
    <div class="category-sub row nomargin-x">
      <a class="category-subitem 
          list-group-item category-item-action col-10 col-md-11 col-xm-11" title="开发经验"
        id="heading-bfd19ce0478d81a44ceb5a9a1aa94202" role="tab" data-toggle="collapse" href="#collapse-bfd19ce0478d81a44ceb5a9a1aa94202"
        aria-expanded="true"
      >
        开发经验
        <span class="list-group-count">(5)</span>
        <i class="iconfont icon-arrowright"></i>
      </a>
      
      <div class="category-collapse collapse show" id="collapse-bfd19ce0478d81a44ceb5a9a1aa94202"
           role="tabpanel" aria-labelledby="heading-bfd19ce0478d81a44ceb5a9a1aa94202">
        
        
          
          
  <div class="category-post-list">
    
    
      
      
        <a href="/deng-2022/blog_2/2023/03/17/Memory%20%E7%94%A8%E6%88%B7%E4%B8%AD%E5%BF%83%E7%B3%BB%E7%BB%9F-%E5%BC%80%E5%8F%91%E6%96%87%E6%A1%A3/" title="Memory 用户中心中心-开发文档"
           class="list-group-item list-group-item-action
           active">
          <span class="category-post">Memory 用户中心中心-开发文档</span>
        </a>
      
    
      
      
        <a href="/deng-2022/blog_2/2023/06/20/Typora%E6%93%8D%E4%BD%9C%E6%8A%80%E5%B7%A7/" title="美轮美奂的文档艺术：Typora的使用技巧和实践"
           class="list-group-item list-group-item-action
           ">
          <span class="category-post">美轮美奂的文档艺术：Typora的使用技巧和实践</span>
        </a>
      
    
      
      
        <a href="/deng-2022/blog_2/2023/08/02/PicMemories%20%E5%A3%81%E7%BA%B8%E5%88%86%E4%BA%AB%E5%B0%8F%E7%A8%8B%E5%BA%8F%20-%20%E5%BC%80%E5%8F%91%E6%96%87%E6%A1%A3/" title="PicMemories-开发文档"
           class="list-group-item list-group-item-action
           ">
          <span class="category-post">PicMemories-开发文档</span>
        </a>
      
    
      
      
        <a href="/deng-2022/blog_2/2023/08/07/TaskMemories%20-%20%E5%BC%80%E5%8F%91%E6%96%87%E6%A1%A3/" title="TaskMemories-开发文档"
           class="list-group-item list-group-item-action
           ">
          <span class="category-post">TaskMemories-开发文档</span>
        </a>
      
    
  </div>

          
  
    
    
    
    <div class="category-sub row nomargin-x">
      <a class="category-subitem collapsed
          list-group-item category-item-action col-10 col-md-11 col-xm-11" title="Elasticsearch"
        id="heading-45e23a169652aaf95ce80da844f3df0d" role="tab" data-toggle="collapse" href="#collapse-45e23a169652aaf95ce80da844f3df0d"
        aria-expanded="false"
      >
        Elasticsearch
        <span class="list-group-count">(1)</span>
        <i class="iconfont icon-arrowright"></i>
      </a>
      
      <div class="category-collapse collapse " id="collapse-45e23a169652aaf95ce80da844f3df0d"
           role="tabpanel" aria-labelledby="heading-45e23a169652aaf95ce80da844f3df0d">
        
        
          
          
  <div class="category-post-list">
    
    
  </div>

          
  
    
    
    
    <div class="category-sub row nomargin-x">
      <a class="category-subitem collapsed
          list-group-item category-item-action col-10 col-md-11 col-xm-11" title="Logstash"
        id="heading-65859d7d6b0c6a5327b8f0b0c09653ad" role="tab" data-toggle="collapse" href="#collapse-65859d7d6b0c6a5327b8f0b0c09653ad"
        aria-expanded="false"
      >
        Logstash
        <span class="list-group-count">(1)</span>
        <i class="iconfont icon-arrowright"></i>
      </a>
      
      <div class="category-collapse collapse " id="collapse-65859d7d6b0c6a5327b8f0b0c09653ad"
           role="tabpanel" aria-labelledby="heading-65859d7d6b0c6a5327b8f0b0c09653ad">
        
        
          
          
  <div class="category-post-list">
    
    
  </div>

          
  
    
    
    
    <div class="category-sub row nomargin-x">
      <a class="category-subitem collapsed
          list-group-item category-item-action col-10 col-md-11 col-xm-11" title="Kibana"
        id="heading-5228fdd6f613af63fc73ee7c02ef8fb5" role="tab" data-toggle="collapse" href="#collapse-5228fdd6f613af63fc73ee7c02ef8fb5"
        aria-expanded="false"
      >
        Kibana
        <span class="list-group-count">(1)</span>
        <i class="iconfont icon-arrowright"></i>
      </a>
      
      <div class="category-collapse collapse " id="collapse-5228fdd6f613af63fc73ee7c02ef8fb5"
           role="tabpanel" aria-labelledby="heading-5228fdd6f613af63fc73ee7c02ef8fb5">
        
        
          
  <div class="category-post-list">
    
    
      
      
        <a href="/deng-2022/blog_2/2023/08/26/MemorySearch%20%E5%BF%86%E6%90%9C%E9%98%81-%E5%BC%80%E5%8F%91%E6%96%87%E6%A1%A3/" title="MemorySearch 忆搜阁-开发文档"
           class="list-group-item list-group-item-action
           ">
          <span class="category-post">MemorySearch 忆搜阁-开发文档</span>
        </a>
      
    
  </div>

        
      </div>
    </div>
  
        
      </div>
    </div>
  
        
      </div>
    </div>
  
        
      </div>
    </div>
  
    
    
    
    <div class="category-sub row nomargin-x">
      <a class="category-subitem collapsed
          list-group-item category-item-action col-10 col-md-11 col-xm-11" title="项目部署"
        id="heading-50be5e3b1697e929a38352262896962e" role="tab" data-toggle="collapse" href="#collapse-50be5e3b1697e929a38352262896962e"
        aria-expanded="false"
      >
        项目部署
        <span class="list-group-count">(1)</span>
        <i class="iconfont icon-arrowright"></i>
      </a>
      
      <div class="category-collapse collapse " id="collapse-50be5e3b1697e929a38352262896962e"
           role="tabpanel" aria-labelledby="heading-50be5e3b1697e929a38352262896962e">
        
        
          
          
  <div class="category-post-list">
    
    
  </div>

          
  
    
    
    
    <div class="category-sub row nomargin-x">
      <a class="category-subitem collapsed
          list-group-item category-item-action col-10 col-md-11 col-xm-11" title="开发经验"
        id="heading-bfd19ce0478d81a44ceb5a9a1aa94202" role="tab" data-toggle="collapse" href="#collapse-bfd19ce0478d81a44ceb5a9a1aa94202"
        aria-expanded="false"
      >
        开发经验
        <span class="list-group-count">(1)</span>
        <i class="iconfont icon-arrowright"></i>
      </a>
      
      <div class="category-collapse collapse " id="collapse-bfd19ce0478d81a44ceb5a9a1aa94202"
           role="tabpanel" aria-labelledby="heading-bfd19ce0478d81a44ceb5a9a1aa94202">
        
        
          
          
  <div class="category-post-list">
    
    
  </div>

          
  
    
    
    
    <div class="category-sub row nomargin-x">
      <a class="category-subitem collapsed
          list-group-item category-item-action col-10 col-md-11 col-xm-11" title="网站"
        id="heading-beb6c0bd8a66cea600c8ce40167eeb57" role="tab" data-toggle="collapse" href="#collapse-beb6c0bd8a66cea600c8ce40167eeb57"
        aria-expanded="false"
      >
        网站
        <span class="list-group-count">(1)</span>
        <i class="iconfont icon-arrowright"></i>
      </a>
      
      <div class="category-collapse collapse " id="collapse-beb6c0bd8a66cea600c8ce40167eeb57"
           role="tabpanel" aria-labelledby="heading-beb6c0bd8a66cea600c8ce40167eeb57">
        
        
          
  <div class="category-post-list">
    
    
      
      
        <a href="/deng-2022/blog_2/2023/06/04/%E6%8F%AD%E7%A7%98%E9%A1%B9%E7%9B%AE%E9%83%A8%E7%BD%B2%E4%B8%8A%E7%BA%BF%E4%B9%8B%E8%B7%AF%EF%BC%9A%E6%B5%81%E7%A8%8B%E3%80%81%E6%8C%91%E6%88%98%E4%B8%8E%E8%A7%A3%E5%86%B3%E6%96%B9%E6%A1%88/" title="揭秘项目部署上线之路：流程、挑战与解决方案"
           class="list-group-item list-group-item-action
           ">
          <span class="category-post">揭秘项目部署上线之路：流程、挑战与解决方案</span>
        </a>
      
    
  </div>

        
      </div>
    </div>
  
        
      </div>
    </div>
  
        
      </div>
    </div>
  
        
      </div>
    </div>
  
</div>


  </aside>


    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">Memory 用户中心中心-开发文档</h1>
            
              <p class="note note-success">
                
                  
                    本文最后更新于：1 年前
                  
                
              </p>
            
            
              <div class="markdown-body">
                
                <h1 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h1><ul>
<li><h5 id="本文档记录了用户中心开发的基本流程，-包含了前期的框架搭建、数据库设计，以及后期的后端接口实现、前端页面开发，实现了基础的登录注册功能，以及查看在线用户状态和删除用户的功能（管理员）"><a href="#本文档记录了用户中心开发的基本流程，-包含了前期的框架搭建、数据库设计，以及后期的后端接口实现、前端页面开发，实现了基础的登录注册功能，以及查看在线用户状态和删除用户的功能（管理员）" class="headerlink" title="本文档记录了用户中心开发的基本流程， 包含了前期的框架搭建、数据库设计，以及后期的后端接口实现、前端页面开发，实现了基础的登录注册功能，以及查看在线用户状态和删除用户的功能（管理员）"></a>本文档记录了用户中心开发的基本流程， 包含了前期的框架搭建、数据库设计，以及后期的后端接口实现、前端页面开发，实现了基础的登录注册功能，以及查看在线用户状态和删除用户的功能（管理员）</h5></li>
</ul>
<h1 id="开发项目前的分析"><a href="#开发项目前的分析" class="headerlink" title="开发项目前的分析"></a>开发项目前的分析</h1><h2 id="企业做项目流程"><a href="#企业做项目流程" class="headerlink" title="企业做项目流程"></a>企业做项目流程</h2><figure class="highlight abnf"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs abnf">需求分析 <span class="hljs-operator">=</span>&gt; 设计（概要设计、详细设计）<span class="hljs-operator">=</span>&gt; 技术选型 <span class="hljs-operator">=</span>&gt; <br>初始化 / 引⼊需要的技术 <span class="hljs-operator">=</span>&gt; 写 Demo <span class="hljs-operator">=</span>&gt; 写代码（实现业务逻辑） <span class="hljs-operator">=</span>&gt;<br>测试（单元测试）<span class="hljs-operator">=</span>&gt; 代码提 交 / 代码评审 <span class="hljs-operator">=</span>&gt; 部署<span class="hljs-operator">=</span>&gt; 发布<br></code></pre></td></tr></table></figure>

<h2 id="需求分析"><a href="#需求分析" class="headerlink" title="需求分析"></a>需求分析</h2><ol>
<li>登录 / 注册  </li>
<li>⽤户管理（仅管理员可⻅）对⽤户的查询或者修改</li>
<li> ⽤户校验（ 仅星球⽤户 ）</li>
</ol>
<h2 id="技术选型"><a href="#技术选型" class="headerlink" title="技术选型"></a>技术选型</h2><h5 id="前端："><a href="#前端：" class="headerlink" title="前端："></a>前端：</h5><p>三件套 + React + 组件库 Ant Design + Umi + Ant Design Pro（现成的管理系统）</p>
<h5 id="后端："><a href="#后端：" class="headerlink" title="后端："></a>后端：</h5><ul>
<li> java </li>
<li>spring（依赖注⼊框架，帮助你管理 Java 对象，集成⼀些其他的内容） </li>
<li>springmvc（web 框架，提供接⼝访问、restful接⼝等能⼒） </li>
<li>mybatis（Java 操作数据库的框架，持久层框架，对 jdbc 的封装）</li>
<li>mybatis-plus（对 mybatis 的增强，不⽤写 sql 也能实现增删改查）</li>
<li> springboot（快速启动 / 快速集成项⽬。不⽤⾃⼰管理 spring 配置，不⽤⾃⼰整合各种框架）</li>
<li> junit 单元测试库 </li>
<li>mysql</li>
</ul>
<h2 id="部署：服务器-容器（平台）"><a href="#部署：服务器-容器（平台）" class="headerlink" title="部署：服务器 / 容器（平台）"></a>部署：服务器 / 容器（平台）</h2><h1 id="框架搭建"><a href="#框架搭建" class="headerlink" title="框架搭建"></a>框架搭建</h1><h3 id="前端框架搭建"><a href="#前端框架搭建" class="headerlink" title="前端框架搭建"></a>前端框架搭建</h3><h5 id="搭建AntDesignPro脚手架"><a href="#搭建AntDesignPro脚手架" class="headerlink" title="搭建AntDesignPro脚手架"></a><a target="_blank" rel="noopener" href="https://blog.csdn.net/Lcq_best/article/details/125569667">搭建AntDesignPro脚手架</a></h5><h3 id="后端框架搭建"><a href="#后端框架搭建" class="headerlink" title="后端框架搭建"></a>后端框架搭建</h3><h1 id="数据库设计"><a href="#数据库设计" class="headerlink" title="数据库设计"></a>数据库设计</h1><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><code class="hljs java">create table <span class="hljs-title function_">user</span><br><span class="hljs-params">(</span><br><span class="hljs-params">    id            bigint                                 not <span class="hljs-literal">null</span> comment <span class="hljs-string">&#x27;id&#x27;</span> primary key,</span><br><span class="hljs-params">    user_account  varchar(<span class="hljs-number">256</span>)</span>                           <span class="hljs-literal">null</span> comment <span class="hljs-string">&#x27;账号&#x27;</span>,<br>    username      <span class="hljs-title function_">varchar</span><span class="hljs-params">(<span class="hljs-number">256</span>)</span>                           <span class="hljs-literal">null</span> comment <span class="hljs-string">&#x27;昵称&#x27;</span>,<br>    user_password <span class="hljs-title function_">varchar</span><span class="hljs-params">(<span class="hljs-number">128</span>)</span>                           not <span class="hljs-literal">null</span> comment <span class="hljs-string">&#x27;密码&#x27;</span>,<br>    avatar_url    <span class="hljs-title function_">varchar</span><span class="hljs-params">(<span class="hljs-number">512</span>)</span>                           <span class="hljs-literal">null</span> comment <span class="hljs-string">&#x27;头像&#x27;</span>,<br>    gender        <span class="hljs-title function_">varchar</span><span class="hljs-params">(<span class="hljs-number">128</span>)</span> <span class="hljs-keyword">default</span> <span class="hljs-string">&#x27;0&#x27;</span>               <span class="hljs-literal">null</span> comment <span class="hljs-string">&#x27;邮箱&#x27;</span>,<br>    phone         <span class="hljs-title function_">varchar</span><span class="hljs-params">(<span class="hljs-number">128</span>)</span>                           <span class="hljs-literal">null</span> comment <span class="hljs-string">&#x27;电话&#x27;</span>,<br>    email         <span class="hljs-title function_">varchar</span><span class="hljs-params">(<span class="hljs-number">128</span>)</span>                           <span class="hljs-literal">null</span> comment <span class="hljs-string">&#x27;邮箱&#x27;</span>,<br>    user_status   <span class="hljs-type">int</span>          <span class="hljs-keyword">default</span> <span class="hljs-number">0</span>                 not <span class="hljs-literal">null</span> comment <span class="hljs-string">&#x27;状态 0 - 正常&#x27;</span>,<br>    create_time   datetime     <span class="hljs-keyword">default</span> CURRENT_TIMESTAMP not <span class="hljs-literal">null</span> comment <span class="hljs-string">&#x27;创建时间&#x27;</span>,<br>    update_time   datetime     <span class="hljs-keyword">default</span> CURRENT_TIMESTAMP not <span class="hljs-literal">null</span> comment <span class="hljs-string">&#x27;更新时间&#x27;</span>,<br>    is_delete     tinyint      <span class="hljs-keyword">default</span> <span class="hljs-number">0</span>                 <span class="hljs-literal">null</span> comment <span class="hljs-string">&#x27;是否删除 0 - 正常&#x27;</span>,<br>    user_role     <span class="hljs-title function_">int</span><span class="hljs-params">(<span class="hljs-number">1</span>)</span>       <span class="hljs-keyword">default</span> <span class="hljs-number">0</span>                 not <span class="hljs-literal">null</span> comment <span class="hljs-string">&#x27;用户权限 0 - 管理员 1 - 普通用户&#x27;</span>,<br>    planet_code   <span class="hljs-title function_">varchar</span><span class="hljs-params">(<span class="hljs-number">512</span>)</span>                           not <span class="hljs-literal">null</span> comment <span class="hljs-string">&#x27;星球编号&#x27;</span><br>)<br>    comment <span class="hljs-string">&#x27;用户&#x27;</span>;<br></code></pre></td></tr></table></figure>

<ul>
<li><h5 id="MybatisX插件快速生成domain-entity-service-以及-serviceImpl"><a href="#MybatisX插件快速生成domain-entity-service-以及-serviceImpl" class="headerlink" title="MybatisX插件快速生成domain entity service 以及 serviceImpl"></a>MybatisX插件快速生成domain entity service 以及 serviceImpl</h5><ul>
<li>选择生成目录</li>
</ul>
<p><img src="C:\Users\Lenovo\AppData\Roaming\Typora\typora-user-images\image-20230324211359229.png" alt="image-20230324211359229"></p>
<ul>
<li>生成选项</li>
</ul>
<p><img src="C:\Users\Lenovo\AppData\Roaming\Typora\typora-user-images\image-20230324211329077.png" alt="image-20230324211329077"></p>
</li>
<li><h5 id="application-yaml-项目配置文件"><a href="#application-yaml-项目配置文件" class="headerlink" title="application.yaml 项目配置文件"></a>application.yaml 项目配置文件</h5><ul>
<li><h5 id="连接到数据库"><a href="#连接到数据库" class="headerlink" title="连接到数据库"></a>连接到数据库</h5></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs java"># DataSource Config<br>datasource:<br>  driver-class-name: com.mysql.cj.jdbc.Driver<br>  url: jdbc:mysql:<span class="hljs-comment">//localhost:3306/memory</span><br>  username: root<br>  password: Dw990831<br></code></pre></td></tr></table></figure>

<ul>
<li><h5 id="项目名称"><a href="#项目名称" class="headerlink" title="项目名称"></a>项目名称</h5></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs java">spring:<br>  # Project name<br>  application:<br>    name: user-center<br></code></pre></td></tr></table></figure>

<ul>
<li><h5 id="项目端口-项目所有API路径上下文"><a href="#项目端口-项目所有API路径上下文" class="headerlink" title="项目端口 | 项目所有API路径上下文"></a>项目端口 | 项目所有API路径上下文</h5></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs java"># 端口<br>server:<br>  port: <span class="hljs-number">8081</span><br>  servlet:<br>    context-path: /api<br></code></pre></td></tr></table></figure>

<ul>
<li><h5 id="逻辑删除-MybatisPlus提供"><a href="#逻辑删除-MybatisPlus提供" class="headerlink" title="逻辑删除(MybatisPlus提供)"></a>逻辑删除(MybatisPlus提供)</h5></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs java"># 逻辑删除<br>mybatis-plus:<br>  global-config:<br>    db-config:<br>      logic-delete-field: isDelete # 全局逻辑删除的实体字段名(since <span class="hljs-number">3.3</span><span class="hljs-number">.0</span>,配置后可以忽略不配置步骤<span class="hljs-number">2</span>)<br>      logic-delete-value: <span class="hljs-number">1</span> # 逻辑已删除值(默认为 <span class="hljs-number">1</span>)<br>      logic-not-delete-value: <span class="hljs-number">0</span> # 逻辑未删除值(默认为 <span class="hljs-number">0</span>)<br></code></pre></td></tr></table></figure>

<ul>
<li><h5 id="session"><a href="#session" class="headerlink" title="session"></a>session</h5></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs java">  # session存活时间 一天<br>session:<br>  timeout: <span class="hljs-number">86640</span><br></code></pre></td></tr></table></figure></li>
</ul>
<h1 id="后端开发"><a href="#后端开发" class="headerlink" title="后端开发"></a>后端开发</h1><h5 id="Service层"><a href="#Service层" class="headerlink" title="Service层"></a>Service层</h5><h2 id="登录-校验"><a href="#登录-校验" class="headerlink" title="登录+校验"></a>登录+校验</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs java">User <span class="hljs-title function_">userLogin</span><span class="hljs-params">(String userAccount, String userPassword, HttpServletRequest request)</span>;<br></code></pre></td></tr></table></figure>

<figure class="highlight reasonml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><code class="hljs reasonml">@Override<br>public User user<span class="hljs-constructor">Login(String <span class="hljs-params">userAccount</span>, String <span class="hljs-params">userPassword</span>, HttpServletRequest <span class="hljs-params">request</span>)</span> &#123;<br>    <span class="hljs-comment">// 1.校验</span><br>    <span class="hljs-comment">// 1.1.账户, 密码不能为空</span><br>    <span class="hljs-keyword">if</span> (<span class="hljs-module-access"><span class="hljs-module"><span class="hljs-identifier">StringUtils</span>.</span></span>is<span class="hljs-constructor">AnyBlank(<span class="hljs-params">userAccount</span>, <span class="hljs-params">userPassword</span>)</span>)<br>        return null;<br><br>    <span class="hljs-comment">// 1.2.账户不小于4位</span><br>    <span class="hljs-keyword">if</span> (userAccount.length<span class="hljs-literal">()</span> &lt; <span class="hljs-number">4</span>)<br>        return null;<br><br>    <span class="hljs-comment">// 1.3.用户密码不小于8位</span><br>    <span class="hljs-keyword">if</span> (userPassword.length<span class="hljs-literal">()</span> &lt; <span class="hljs-number">8</span>)<br>        return null;<br><br>    <span class="hljs-comment">// 1.4.账户不包含特殊字符</span><br>    String pattern = <span class="hljs-string">&quot;.*[\\s`~!@#$%^&amp;*()+=|&#123;&#125;&#x27;:;&#x27;,\\[\\].&lt;&gt;/?~！@#￥%……&amp;*（）——+|&#123;&#125;【】‘；：”“’。，、？\\\\]+.*&quot;</span>;<br>    <span class="hljs-keyword">if</span> (<span class="hljs-module-access"><span class="hljs-module"><span class="hljs-identifier">Pattern</span>.</span></span>matches(pattern, userAccount))<br>        return null;<br><br>    <span class="hljs-comment">// 1.5.检验该用户是否注册</span><br>    User user = <span class="hljs-keyword">new</span> <span class="hljs-constructor">User()</span>;<br><br>    user.set<span class="hljs-constructor">UserAccount(<span class="hljs-params">userAccount</span>)</span>;<br><br>    String encryptPassword = <span class="hljs-module-access"><span class="hljs-module"><span class="hljs-identifier">DigestUtils</span>.</span></span>md5<span class="hljs-constructor">DigestAsHex((SALT + <span class="hljs-params">userPassword</span>)</span>.get<span class="hljs-constructor">Bytes()</span>);<br>    user.set<span class="hljs-constructor">UserPassword(<span class="hljs-params">encryptPassword</span>)</span>;<br><br>    QueryWrapper&lt;User&gt; qw = <span class="hljs-keyword">new</span> QueryWrapper&lt;&gt;<span class="hljs-literal">()</span>;<br>    qw.eq(<span class="hljs-string">&quot;user_account&quot;</span>, userAccount).eq(<span class="hljs-string">&quot;user_password&quot;</span>, encryptPassword);<br>    User one = this.get<span class="hljs-constructor">One(<span class="hljs-params">qw</span>)</span>;<br><br>    <span class="hljs-comment">// 1.5.1.用户未注册(包含了MP自带的逻辑删除校验)</span><br>    <span class="hljs-keyword">if</span> (one<span class="hljs-operator"> == </span>null)<br>        return null;<br><br>    <span class="hljs-comment">// 2.脱敏用户信息</span><br>    User safetyUser = get<span class="hljs-constructor">SafetyUser(<span class="hljs-params">one</span>)</span>;<br><br>    <span class="hljs-comment">// 3.记录用户登录态</span><br>    request.get<span class="hljs-constructor">Session()</span>.set<span class="hljs-constructor">Attribute(USER_LOGIN_STATE, <span class="hljs-params">safetyUser</span>)</span>;<br><br>    <span class="hljs-comment">// 4.返回用户信息</span><br>    return safetyUser;<br>&#125;<br></code></pre></td></tr></table></figure>

<h2 id="注册-校验"><a href="#注册-校验" class="headerlink" title="注册+校验"></a>注册+校验</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-type">long</span> <span class="hljs-title function_">userRegister</span><span class="hljs-params">(String userAccount, String userPassword, String checkPassword)</span>;<br></code></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 用户注册</span><br><span class="hljs-comment">     *</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> userAccount   账户</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> userPassword  密码</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> checkPassword 二次密码</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span> 用户id</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-type">long</span> <span class="hljs-title function_">userRegister</span><span class="hljs-params">(String userAccount, String userPassword, String checkPassword)</span> &#123;<br>        <span class="hljs-comment">// 1.校验</span><br>        <span class="hljs-comment">// 1.1.账户, 密码, 二次密码不能为空</span><br>        <span class="hljs-keyword">if</span> (StringUtils.isAnyBlank(userAccount, userPassword, checkPassword))<br>            <span class="hljs-keyword">return</span> -<span class="hljs-number">1</span>;<br><br>        <span class="hljs-comment">// 1.2.账户不小于4位</span><br>        <span class="hljs-keyword">if</span> (userAccount.length() &lt; <span class="hljs-number">4</span>)<br>            <span class="hljs-keyword">return</span> -<span class="hljs-number">1</span>;<br><br>        <span class="hljs-comment">// 1.3.账户不能重复</span><br>        QueryWrapper&lt;User&gt; lqw = <span class="hljs-keyword">new</span> <span class="hljs-title class_">QueryWrapper</span>&lt;&gt;(); <span class="hljs-comment">// LambdaQueryWrapper&lt;User&gt; userLambdaQueryWrapper = new LambdaQueryWrapper&lt;&gt;();</span><br>        lqw.eq(<span class="hljs-string">&quot;user_account&quot;</span>, userAccount); <span class="hljs-comment">// userLambdaQueryWrapper.eq(User::getUserAccount, userAccount);</span><br>        <span class="hljs-type">Long</span> <span class="hljs-variable">count</span> <span class="hljs-operator">=</span> userMapper.selectCount(lqw);   <span class="hljs-comment">// long count = this.count(lqw);</span><br><br>        <span class="hljs-keyword">if</span> (count &gt; <span class="hljs-number">0</span>)<br>            <span class="hljs-keyword">return</span> -<span class="hljs-number">1</span>;<br><br>        <span class="hljs-comment">// 1.4.用户密码不小于8位</span><br>        <span class="hljs-keyword">if</span> (userPassword.length() &lt; <span class="hljs-number">8</span>)<br>            <span class="hljs-keyword">return</span> -<span class="hljs-number">1</span>;<br><br>        <span class="hljs-comment">// 1.5.账户不包含特殊字符</span><br>        <span class="hljs-type">String</span> <span class="hljs-variable">pattern</span> <span class="hljs-operator">=</span> <span class="hljs-string">&quot;.*[\\s`~!@#$%^&amp;*()+=|&#123;&#125;&#x27;:;&#x27;,\\[\\].&lt;&gt;/?~！@#￥%……&amp;*（）——+|&#123;&#125;【】‘；：”“’。，、？\\\\]+.*&quot;</span>;<br>        <span class="hljs-keyword">if</span> (Pattern.matches(pattern, userAccount))<br>            <span class="hljs-keyword">return</span> -<span class="hljs-number">1</span>;<br><br>        <span class="hljs-comment">// 1.6.二次密码与密码相同</span><br>        <span class="hljs-keyword">if</span> (!userPassword.equals(checkPassword))<br>            <span class="hljs-keyword">return</span> -<span class="hljs-number">1</span>;<br><br><br>        <span class="hljs-comment">// 2.对密码进行加密</span><br>        <span class="hljs-type">String</span> <span class="hljs-variable">encryptPassword</span> <span class="hljs-operator">=</span> DigestUtils.md5DigestAsHex((SALT + userPassword).getBytes());<br><span class="hljs-comment">//        System.out.println(encryptPassword);</span><br><br><br>        <span class="hljs-comment">// 3.向数据库中插入用户数据</span><br>        <span class="hljs-type">User</span> <span class="hljs-variable">user</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">User</span>();<br>        <span class="hljs-comment">//</span><br>        user.setUserAccount(userAccount);<br>        user.setUserPassword(encryptPassword);<br>        <span class="hljs-type">boolean</span> <span class="hljs-variable">save</span> <span class="hljs-operator">=</span> <span class="hljs-built_in">this</span>.save(user);<br>        <span class="hljs-comment">//插入失败</span><br>        <span class="hljs-keyword">if</span> (!save)<br>            <span class="hljs-keyword">return</span> -<span class="hljs-number">1</span>;<br><br>        <span class="hljs-keyword">return</span> user.getId();<br>    &#125;<br></code></pre></td></tr></table></figure>

<h2 id="封装脱敏用户信息"><a href="#封装脱敏用户信息" class="headerlink" title="封装脱敏用户信息"></a>封装脱敏用户信息</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs java">User <span class="hljs-title function_">getSafetyUser</span><span class="hljs-params">(User originUser)</span>;<br></code></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * 用户信息脱敏</span><br><span class="hljs-comment"> *</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@param</span> originUser 原始用户</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@return</span> 脱敏后的用户</span><br><span class="hljs-comment"> */</span><br><span class="hljs-keyword">public</span> User <span class="hljs-title function_">getSafetyUser</span><span class="hljs-params">(User originUser)</span> &#123;<br>    <span class="hljs-keyword">if</span> (originUser == <span class="hljs-literal">null</span>)<br>        <span class="hljs-keyword">return</span> <span class="hljs-literal">null</span>;<br><br>    <span class="hljs-type">User</span> <span class="hljs-variable">safetyUser</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">User</span>();<br>    safetyUser.setId(originUser.getId());<br>    safetyUser.setUserAccount(originUser.getUserAccount());<br>    safetyUser.setUsername(originUser.getUsername());<br>    safetyUser.setAvatarUrl(originUser.getAvatarUrl());<br>    safetyUser.setGender(originUser.getGender());<br>    safetyUser.setPhone(originUser.getPhone());<br>    safetyUser.setEmail(originUser.getEmail());<br>    safetyUser.setUserStatus(originUser.getUserStatus());<br>    safetyUser.setCreateTime(originUser.getCreateTime());<br>    safetyUser.setIsDelete(originUser.getIsDelete());<br>    safetyUser.setUserRole(originUser.getUserRole());<br><br>    <span class="hljs-keyword">return</span> safetyUser;<br>&#125;<br></code></pre></td></tr></table></figure>

<h5 id="Controller层"><a href="#Controller层" class="headerlink" title="Controller层"></a>Controller层</h5><h2 id="登录"><a href="#登录" class="headerlink" title="登录"></a>登录</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 用户登录</span><br><span class="hljs-comment">     *</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> userLoginRequest 登录信息封装</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> request          request</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span> User</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-meta">@PostMapping(&quot;/login&quot;)</span><br>    <span class="hljs-keyword">public</span> User <span class="hljs-title function_">userLogin</span><span class="hljs-params">(<span class="hljs-meta">@RequestBody</span> UserLoginRequest userLoginRequest, HttpServletRequest request)</span> &#123;<br>        <span class="hljs-type">String</span> <span class="hljs-variable">userAccount</span> <span class="hljs-operator">=</span> userLoginRequest.getUserAccount();<br>        <span class="hljs-type">String</span> <span class="hljs-variable">userPassword</span> <span class="hljs-operator">=</span> userLoginRequest.getUserPassword();<br>        <span class="hljs-comment">//controller对参数的校验</span><br>        <span class="hljs-keyword">if</span> (StringUtils.isAnyBlank(userAccount, userPassword))<br>            <span class="hljs-keyword">return</span> <span class="hljs-literal">null</span>;<br><br>        <span class="hljs-keyword">return</span> userService.userLogin(userAccount, userPassword, request);<br>    &#125;<br></code></pre></td></tr></table></figure>

<h2 id="注册"><a href="#注册" class="headerlink" title="注册"></a>注册</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * 用户注册</span><br><span class="hljs-comment"> *</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@param</span> userRegisterRequest 注册信息封装类</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@return</span> id</span><br><span class="hljs-comment"> */</span><br><span class="hljs-meta">@PostMapping(&quot;/register&quot;)</span><br><span class="hljs-keyword">public</span> Long <span class="hljs-title function_">userRegister</span><span class="hljs-params">(<span class="hljs-meta">@RequestBody</span> UserRegisterRequest userRegisterRequest)</span> &#123;<br>    <span class="hljs-type">String</span> <span class="hljs-variable">userAccount</span> <span class="hljs-operator">=</span> userRegisterRequest.getUserAccount();<br>    <span class="hljs-type">String</span> <span class="hljs-variable">userPassword</span> <span class="hljs-operator">=</span> userRegisterRequest.getUserPassword();<br>    <span class="hljs-type">String</span> <span class="hljs-variable">checkPassword</span> <span class="hljs-operator">=</span> userRegisterRequest.getCheckPassword();<br>    <span class="hljs-comment">//controller对参数的校验</span><br>    <span class="hljs-keyword">if</span> (StringUtils.isAnyBlank(userAccount, userPassword, checkPassword))<br>        <span class="hljs-keyword">return</span> <span class="hljs-literal">null</span>;<br><br>    <span class="hljs-keyword">return</span> userService.userRegister(userAccount, userPassword, checkPassword);<br>&#125;<br></code></pre></td></tr></table></figure>

<h3 id="查询用户-权限校验"><a href="#查询用户-权限校验" class="headerlink" title="查询用户+权限校验"></a>查询用户+权限校验</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 查询用户</span><br><span class="hljs-comment">     *</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> username 用户名</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span> 查到的用户</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-meta">@GetMapping(&quot;/search&quot;)</span><br>    <span class="hljs-keyword">public</span> List&lt;User&gt; <span class="hljs-title function_">userSearch</span><span class="hljs-params">(String username, HttpServletRequest request)</span> &#123;<br>        <span class="hljs-comment">// 1.校验权限</span><br>        <span class="hljs-keyword">if</span> (!isAdmin(request))<br>            <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayList</span>&lt;&gt;();<br>        <span class="hljs-comment">// 2.判空, 默认查询全部</span><br>        QueryWrapper&lt;User&gt; qw = <span class="hljs-keyword">new</span> <span class="hljs-title class_">QueryWrapper</span>&lt;&gt;();<br>        <span class="hljs-keyword">if</span> (StringUtils.isNotBlank(username))<br>            qw.like(<span class="hljs-string">&quot;username&quot;</span>, username);<br>        <span class="hljs-comment">// 3.查询</span><br>        List&lt;User&gt; userList = userService.list(qw);<br>        <span class="hljs-comment">// 4.返回脱敏的用户信息</span><br>        <span class="hljs-keyword">return</span> userList.stream().map(user -&gt; userService.getSafetyUser(user)).collect(Collectors.toList());<br>    &#125;<br></code></pre></td></tr></table></figure>

<h3 id="删除用户-权限校验"><a href="#删除用户-权限校验" class="headerlink" title="删除用户+权限校验"></a>删除用户+权限校验</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 删除用户</span><br><span class="hljs-comment">     *</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> id 用户id</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span> true/false</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-meta">@DeleteMapping(&quot;/delete&quot;)</span><br>    <span class="hljs-keyword">public</span> Boolean <span class="hljs-title function_">userDelete</span><span class="hljs-params">(Long id, HttpServletRequest request)</span> &#123;<br>        <span class="hljs-comment">// 1.校验权限</span><br>        <span class="hljs-keyword">if</span> (!isAdmin(request))<br>            <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;<br><br>        <span class="hljs-keyword">if</span> (id &lt;= <span class="hljs-number">0</span>)<br>            <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;<br><br>        <span class="hljs-comment">// 2.删除用户(只要配置MP的逻辑删除的话, 该删除为逻辑删除)</span><br>        <span class="hljs-keyword">return</span> userService.removeById(id);<br>    &#125;<br><br></code></pre></td></tr></table></figure>

<h2 id="获取用户登录态"><a href="#获取用户登录态" class="headerlink" title="获取用户登录态"></a>获取用户登录态</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">   * 查询用户</span><br><span class="hljs-comment">   *</span><br><span class="hljs-comment">   * <span class="hljs-doctag">@param</span> username 用户名</span><br><span class="hljs-comment">   * <span class="hljs-doctag">@return</span> 查到的用户</span><br><span class="hljs-comment">   */</span><br>  <span class="hljs-meta">@GetMapping(&quot;/search&quot;)</span><br>  <span class="hljs-keyword">public</span> List&lt;User&gt; <span class="hljs-title function_">userSearch</span><span class="hljs-params">(String username, HttpServletRequest request)</span> &#123;<br>      <span class="hljs-comment">// 1.校验权限</span><br>      <span class="hljs-keyword">if</span> (!isAdmin(request))<br>          <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayList</span>&lt;&gt;();<br>      <span class="hljs-comment">// 2.判空, 默认查询全部</span><br>      QueryWrapper&lt;User&gt; qw = <span class="hljs-keyword">new</span> <span class="hljs-title class_">QueryWrapper</span>&lt;&gt;();<br>      <span class="hljs-keyword">if</span> (StringUtils.isNotBlank(username))<br>          qw.like(<span class="hljs-string">&quot;username&quot;</span>, username);<br>      <span class="hljs-comment">// 3.查询</span><br>      List&lt;User&gt; userList = userService.list(qw);<br>      <span class="hljs-comment">// 4.返回脱敏的用户信息</span><br>      <span class="hljs-keyword">return</span> userList.stream().map(user -&gt; userService.getSafetyUser(user)).collect(Collectors.toList());<br>  &#125;<br></code></pre></td></tr></table></figure>

<h2 id="封装校验管理员逻辑"><a href="#封装校验管理员逻辑" class="headerlink" title="封装校验管理员逻辑"></a>封装校验管理员逻辑</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 校验是否为管理员</span><br><span class="hljs-comment">     *</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> request request</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span> 校验成功与否</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> Boolean <span class="hljs-title function_">isAdmin</span><span class="hljs-params">(HttpServletRequest request)</span> &#123;<br>        <span class="hljs-comment">//校验是否为管理员</span><br>        <span class="hljs-type">User</span> <span class="hljs-variable">user</span> <span class="hljs-operator">=</span> (User) request.getSession().getAttribute(USER_LOGIN_STATE);<br>        <span class="hljs-keyword">return</span> user != <span class="hljs-literal">null</span> &amp;&amp; user.getUserRole() == ADMIN_ROLE;<br>    &#125;<br></code></pre></td></tr></table></figure>

<h5 id="constant层"><a href="#constant层" class="headerlink" title="constant层"></a>constant层</h5><h2 id="封装常量"><a href="#封装常量" class="headerlink" title="封装常量"></a>封装常量</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">//登录用户session Key</span><br>   <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> <span class="hljs-type">String</span> <span class="hljs-variable">USER_LOGIN_STATE</span> <span class="hljs-operator">=</span> <span class="hljs-string">&quot;userLoginState&quot;</span>;<br>   <span class="hljs-comment">//管理员权限</span><br>   <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> <span class="hljs-type">int</span> <span class="hljs-variable">ADMIN_ROLE</span> <span class="hljs-operator">=</span> <span class="hljs-number">1</span>;<br>   <span class="hljs-comment">//普通用户权限</span><br>   <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> <span class="hljs-type">int</span> <span class="hljs-variable">DEFAULT_ROLE</span> <span class="hljs-operator">=</span> <span class="hljs-number">1</span>;<br></code></pre></td></tr></table></figure>

<h5 id="model-request层"><a href="#model-request层" class="headerlink" title="model/request层"></a>model/request层</h5><h2 id="封装login-register实体接收类"><a href="#封装login-register实体接收类" class="headerlink" title="封装login/register实体接收类"></a>封装login/register实体接收类</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@Data</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">UserLoginRequest</span> &#123;<br>    <span class="hljs-keyword">private</span> String userAccount;<br>    <span class="hljs-keyword">private</span> String userPassword;<br>&#125;<br></code></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@Data</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">UserRegisterRequest</span> &#123;<br>    <span class="hljs-keyword">private</span> String userAccount;<br>    <span class="hljs-keyword">private</span> String userPassword;<br>    <span class="hljs-keyword">private</span> String checkPassword;<br>&#125;<br></code></pre></td></tr></table></figure>

<h1 id="前端开发"><a href="#前端开发" class="headerlink" title="前端开发"></a>前端开发</h1><h2 id="修改登录页面"><a href="#修改登录页面" class="headerlink" title="修改登录页面"></a>修改登录页面</h2><ul>
<li><p>熟悉登录流程 请求地址 返回数据</p>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs typescript"><span class="hljs-comment">// 登录</span><br><span class="hljs-keyword">const</span> user = <span class="hljs-keyword">await</span> <span class="hljs-title function_">login</span>(&#123;<br>  ...values,<br>  <span class="hljs-keyword">type</span>,<br>&#125;);<br></code></pre></td></tr></table></figure>

<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs typescript"><span class="hljs-comment">/** 登录接口 POST /api/login/account */</span><br><span class="hljs-keyword">export</span> <span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title function_">login</span>(<span class="hljs-params">body: API.LoginParams, options?: &#123; [key: <span class="hljs-built_in">string</span>]: <span class="hljs-built_in">any</span> &#125;</span>) &#123;<br>  <span class="hljs-keyword">return</span> request&lt;<span class="hljs-variable constant_">API</span>.<span class="hljs-property">LoginResult</span>&gt;(<span class="hljs-string">&#x27;/api/user/login&#x27;</span>, &#123;<br>    <span class="hljs-attr">method</span>: <span class="hljs-string">&#x27;POST&#x27;</span>,<br>    <span class="hljs-attr">headers</span>: &#123;<br>      <span class="hljs-string">&#x27;Content-Type&#x27;</span>: <span class="hljs-string">&#x27;application/json&#x27;</span>,<br>    &#125;,<br>    <span class="hljs-attr">data</span>: body,<br>    ...(options || &#123;&#125;),<br>  &#125;);<br>&#125;<br></code></pre></td></tr></table></figure></li>
<li><p>登录表单的校验逻辑(账号 密码)</p>
</li>
<li><p>登录校验  成功则提示登录成功   重定向到welcome页面</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-comment">// 登录成功</span><br><span class="hljs-keyword">if</span> (user) &#123;<br>  <span class="hljs-keyword">const</span> defaultLoginSuccessMessage = <span class="hljs-string">&#x27;登录成功！&#x27;</span>;<br>  message.<span class="hljs-title function_">success</span>(defaultLoginSuccessMessage);<br>  <span class="hljs-keyword">await</span> <span class="hljs-title function_">fetchUserInfo</span>();<br>  <span class="hljs-comment">/** 此方法会跳转到 redirect 参数所在的位置 */</span><br>  <span class="hljs-keyword">if</span> (!history) <span class="hljs-keyword">return</span>;<br>  <span class="hljs-keyword">const</span> &#123;query&#125; = history.<span class="hljs-property">location</span>;<br>  <span class="hljs-keyword">const</span> &#123;redirect&#125; = query <span class="hljs-keyword">as</span> &#123;<br>    <span class="hljs-attr">redirect</span>: string;<br>  &#125;;<br>  <span class="hljs-comment">// &#123; path: &#x27;/&#x27;, redirect: &#x27;/welcome&#x27; &#125;,</span><br>  <span class="hljs-comment">// 跳转到欢迎页面</span><br>  history.<span class="hljs-title function_">push</span>(redirect || <span class="hljs-string">&#x27;/&#x27;</span>);<br>  <span class="hljs-keyword">return</span>;<br>&#125;<br></code></pre></td></tr></table></figure></li>
<li><p>登录校验  失败则提示登录失败</p>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs typescript"><span class="hljs-keyword">const</span> defaultLoginFailureMessage = <span class="hljs-string">&#x27;登录失败，请重试！&#x27;</span>;<br>message.<span class="hljs-title function_">error</span>(defaultLoginFailureMessage);<br></code></pre></td></tr></table></figure></li>
</ul>
<h2 id="开发注册页面"><a href="#开发注册页面" class="headerlink" title="开发注册页面"></a>开发注册页面</h2><ul>
<li><p>登录页的复制粘贴</p>
</li>
<li><p>路由的理解  设置注册页的路由  </p>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs typescript">&#123; <span class="hljs-attr">name</span>: <span class="hljs-string">&#x27;注册&#x27;</span>, <span class="hljs-attr">path</span>: <span class="hljs-string">&#x27;/user/register&#x27;</span>, <span class="hljs-attr">component</span>: <span class="hljs-string">&#x27;./user/Register&#x27;</span> &#125;,<br></code></pre></td></tr></table></figure></li>
<li><p>注册表单的校验逻辑(账号 密码)</p>
</li>
<li><p>熟悉注册流程  请求地址 返回数据</p>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs typescript"><span class="hljs-comment">// 发起请求</span><br><span class="hljs-keyword">const</span> id = <span class="hljs-keyword">await</span> <span class="hljs-title function_">register</span>(&#123;<br>  ...values,<br>  <span class="hljs-keyword">type</span>,<br>&#125;);<br></code></pre></td></tr></table></figure>

<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs typescript"><span class="hljs-comment">/** 注册接口 POST /api/login/account */</span><br><span class="hljs-keyword">export</span> <span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title function_">register</span>(<span class="hljs-params">body: API.LoginParams, options?: &#123; [key: <span class="hljs-built_in">string</span>]: <span class="hljs-built_in">any</span> &#125;</span>) &#123;<br>  <span class="hljs-keyword">return</span> request&lt;<span class="hljs-variable constant_">API</span>.<span class="hljs-property">LoginResult</span>&gt;(<span class="hljs-string">&#x27;/api/user/register&#x27;</span>, &#123;<br>    <span class="hljs-attr">method</span>: <span class="hljs-string">&#x27;POST&#x27;</span>,<br>    <span class="hljs-attr">headers</span>: &#123;<br>      <span class="hljs-string">&#x27;Content-Type&#x27;</span>: <span class="hljs-string">&#x27;application/json&#x27;</span>,<br>    &#125;,<br>    <span class="hljs-attr">data</span>: body,<br>    ...(options || &#123;&#125;),<br>  &#125;);<br>&#125;<br></code></pre></td></tr></table></figure></li>
<li><p>解决访问注册页面   重定向到Login问题   业务逻辑   白名单内, 无需重定向到登录页</p>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs typescript"><span class="hljs-comment">//白名单内无需重定向</span><br><span class="hljs-keyword">if</span> (<span class="hljs-variable constant_">NO_NEED_LOGIN_WHITE_LIST</span>.<span class="hljs-title function_">includes</span>(location.<span class="hljs-property">pathname</span>))<br>  <span class="hljs-keyword">return</span>;<br><span class="hljs-comment">// 白名单外, 如果没有登录, 重定向到 login</span><br><span class="hljs-keyword">if</span> (!initialState?.<span class="hljs-property">currentUser</span>) &#123;<br>  history.<span class="hljs-title function_">push</span>(loginPath);<br>&#125;<br></code></pre></td></tr></table></figure></li>
<li><p>注册成功  提示”注册成功”  跳转登录页面</p>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs typescript">history.<span class="hljs-title function_">push</span>(&#123;<br>  <span class="hljs-attr">pathname</span>: <span class="hljs-string">&#x27;user/login&#x27;</span>,<br>  query,<br>&#125;)<br>跳转到登录页面<br></code></pre></td></tr></table></figure></li>
<li><p>注册失败  提示”注册失败”   </p>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs typescript"><span class="hljs-keyword">const</span> defaultLoginFailureMessage = <span class="hljs-string">&#x27;注册失败，请重试！&#x27;</span>;<br>message.<span class="hljs-title function_">error</span>(defaultLoginFailureMessage);<br></code></pre></td></tr></table></figure></li>
<li><p>“登录” 修改为 “注册”   (了解源码)</p>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs typescript">submitter=&#123;&#123;<br>  <span class="hljs-attr">searchConfig</span>: &#123;<br>    <span class="hljs-attr">submitText</span>: <span class="hljs-string">&#x27;注册&#x27;</span><br>  &#125;<br>&#125;&#125;<br></code></pre></td></tr></table></figure></li>
<li><p>添加注册校验  简单的逻辑  根据返回的数据  解构出密码和二次密码 判断二者是否相等</p>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs typescript"><span class="hljs-keyword">const</span> &#123;userPassword, checkPassword&#125; = values;<br><span class="hljs-comment">//校验</span><br><span class="hljs-keyword">if</span> (userPassword != checkPassword) &#123;<br>  message.<span class="hljs-title function_">error</span>(<span class="hljs-string">&#x27;两次输入的密码不一致!&#x27;</span>)<br>  <span class="hljs-keyword">return</span>;<br>&#125;<br></code></pre></td></tr></table></figure></li>
<li><p>添加登录页跳转到注册页的链接”新用户注册”  仿照  “忘记密码”  </p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs vue">&lt;a<br>  href=&quot;/user/register&quot;<br>  target=&quot;_blank&quot;<br>  rel=&quot;noreferrer&quot;<br>&gt;<br>  新用户注册<br>&lt;/a&gt;<br></code></pre></td></tr></table></figure></li>
</ul>
<h1 id="获取当前用户登录态"><a href="#获取当前用户登录态" class="headerlink" title="获取当前用户登录态"></a>获取当前用户登录态</h1><h2 id="后端实现接口"><a href="#后端实现接口" class="headerlink" title="后端实现接口"></a>后端实现接口</h2><p>返回当前用户的当前信息(重新查询过数据库)</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@GetMapping(&quot;/currentUser&quot;)</span><br><span class="hljs-keyword">public</span> User <span class="hljs-title function_">getCurrentUser</span><span class="hljs-params">(HttpServletRequest request)</span> &#123;<br>    <span class="hljs-type">User</span> <span class="hljs-variable">currentUser</span> <span class="hljs-operator">=</span> (User) request.getSession().getAttribute(USER_LOGIN_STATE);<br>    <span class="hljs-keyword">if</span> (currentUser == <span class="hljs-literal">null</span>)<br>        <span class="hljs-keyword">return</span> <span class="hljs-literal">null</span>;<br><br>    <span class="hljs-type">Long</span> <span class="hljs-variable">id</span> <span class="hljs-operator">=</span> currentUser.getId();<br>    <span class="hljs-comment">// 查询数据库, 获取最新信息, 而非登录时记录的信息</span><br>    <span class="hljs-keyword">return</span> userService.getById(id);<br>&#125;<br></code></pre></td></tr></table></figure>

<h2 id="前端获取用户登录态"><a href="#前端获取用户登录态" class="headerlink" title="前端获取用户登录态"></a>前端获取用户登录态</h2><ul>
<li>app.tsx   前端服务入口   每次打开页面, 都会执行查询</li>
</ul>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs typescript"><span class="hljs-comment">// 查询用户登录态信息</span><br><span class="hljs-keyword">return</span> <span class="hljs-keyword">await</span> <span class="hljs-title function_">queryCurrentUser</span>();<br></code></pre></td></tr></table></figure>

<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs typescript"><span class="hljs-comment">// 请求</span><br><span class="hljs-keyword">return</span> request&lt;<span class="hljs-variable constant_">API</span>.<span class="hljs-property">CurrentUser</span>&gt;(<span class="hljs-string">&#x27;/api/user/currentUser&#x27;</span>, &#123;<br>  <span class="hljs-attr">method</span>: <span class="hljs-string">&#x27;GET&#x27;</span>,<br>  ...(options || &#123;&#125;),<br>&#125;);<br></code></pre></td></tr></table></figure>

<ul>
<li>修改CurrentUser, 将返回的字段全部修改为对应数据库中的字段</li>
</ul>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs typescript"><span class="hljs-keyword">type</span> <span class="hljs-title class_">CurrentUser</span> = &#123;<br>  id?: <span class="hljs-built_in">number</span>;<br>  userAccount?: <span class="hljs-built_in">string</span>;<br>  username?: <span class="hljs-built_in">string</span>;<br>  avatarUtil?: <span class="hljs-built_in">string</span>;<br>  gender?: <span class="hljs-built_in">string</span>;<br>  phone?: <span class="hljs-built_in">string</span>;<br>  email?: <span class="hljs-built_in">string</span>;<br>  userStatus?: <span class="hljs-built_in">string</span>;<br>  userRole?: <span class="hljs-built_in">string</span>;<br>&#125;;<br></code></pre></td></tr></table></figure>

<ul>
<li>设置白名单, 登录注册页面不会返回查询到的用户登录态, 其余页面会返回查询到的用户登录态</li>
</ul>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><code class="hljs typescript"><span class="hljs-comment">// 白名单内(登陆注册), 不返回用户登录态信息</span><br><span class="hljs-keyword">if</span> (<span class="hljs-variable constant_">NO_NEED_LOGIN_WHITE_LIST</span>.<span class="hljs-title function_">includes</span>(history.<span class="hljs-property">location</span>.<span class="hljs-property">pathname</span>)) &#123;<br>  <span class="hljs-keyword">return</span> &#123;<br>    fetchUserInfo,<br>    <span class="hljs-attr">settings</span>: defaultSettings,<br>  &#125;;<br>&#125;<br><span class="hljs-comment">// 登陆注册后, 页面返回用户登录态信息</span><br><span class="hljs-keyword">const</span> currentUser = <span class="hljs-keyword">await</span> <span class="hljs-title function_">fetchUserInfo</span>();<br><span class="hljs-keyword">return</span> &#123;<br>  fetchUserInfo,<br>  currentUser,<br>  <span class="hljs-attr">settings</span>: defaultSettings,<br>&#125;;<br></code></pre></td></tr></table></figure>

<h1 id="开发欢迎页面"><a href="#开发欢迎页面" class="headerlink" title="开发欢迎页面"></a>开发欢迎页面</h1><ul>
<li>设置欢迎页面的水印  头像</li>
</ul>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs typescript"><span class="hljs-attr">waterMarkProps</span>: &#123;<br>  <span class="hljs-comment">// 添加水印</span><br>  <span class="hljs-attr">content</span>: initialState?.<span class="hljs-property">currentUser</span>?.<span class="hljs-property">username</span>, <span class="hljs-comment">// 以用户昵称作为水印</span><br>&#125;,<br></code></pre></td></tr></table></figure>

<ul>
<li>头像的话在/src/components/RightContext/AvatarDropdown.tsx里有个引用</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs vue">&lt;Avatar size=&quot;small&quot; className=&#123;styles.avatar&#125; src=&#123;currentUser.avatarUrl&#125; alt=&quot;avatar&quot;/&gt;<br></code></pre></td></tr></table></figure>

<ul>
<li>我这边头像刷新不出来是因为数据库里字段名写成avatarUtil了,一直没发现,改了正确的字段名以及映射实体类属性名  Mapper.xml 文件后 头像映射正常了</li>
</ul>
<h1 id="开发用户管理页面"><a href="#开发用户管理页面" class="headerlink" title="开发用户管理页面"></a>开发用户管理页面</h1><h2 id="新建一个管理界面"><a href="#新建一个管理界面" class="headerlink" title="新建一个管理界面"></a>新建一个管理界面</h2><ul>
<li><p>他奶奶的我这边出问题了</p>
</li>
<li><p>我新建了一个/Pages/Admin/UserManage    把Register文件夹复制过去打算修改, 结果它给我把Register的路由给替换了</p>
</li>
</ul>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs typescript">&#123; <span class="hljs-attr">name</span>: <span class="hljs-string">&#x27;注册&#x27;</span>, <span class="hljs-attr">path</span>: <span class="hljs-string">&#x27;/user/register&#x27;</span>, <span class="hljs-attr">component</span>: <span class="hljs-string">&#x27;./user/UserManage&#x27;</span> &#125;,<br></code></pre></td></tr></table></figure>

<ul>
<li>然后前端直接挂掉了, 报错报了这个玩意儿  妈的找了半天 终于发现了 把路由改回来了</li>
</ul>
<figure class="highlight lua"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs lua">Cannot <span class="hljs-built_in">find</span> <span class="hljs-built_in">module</span> <span class="hljs-string">&#x27;D:/Project/星球项目/ClientCenter/myapp/src/pages/user/UserManage&#x27;</span><br></code></pre></td></tr></table></figure>

<ul>
<li>给新增的用户管理页面加个路由</li>
</ul>
<p>​    path: ‘/admin/user-manager’    是访问路径</p>
<p>​    component: ‘./Admin/UserManage’    是资源路径</p>
<p>​    仿照下面的写就行了</p>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><code class="hljs typescript">&#123;<br>  <span class="hljs-attr">path</span>: <span class="hljs-string">&#x27;/admin&#x27;</span>,<br>  <span class="hljs-attr">name</span>: <span class="hljs-string">&#x27;管理页&#x27;</span>,<br>  <span class="hljs-attr">icon</span>: <span class="hljs-string">&#x27;crown&#x27;</span>,<br>  <span class="hljs-attr">access</span>: <span class="hljs-string">&#x27;canAdmin&#x27;</span>,<br>  <span class="hljs-attr">component</span>: <span class="hljs-string">&#x27;./Admin&#x27;</span>,<br>  <span class="hljs-attr">routes</span>: [<br>  <span class="hljs-comment">// 用户管理</span><br>    &#123; <span class="hljs-attr">path</span>: <span class="hljs-string">&#x27;/admin/user-manager&#x27;</span>, <span class="hljs-attr">name</span>: <span class="hljs-string">&#x27;用户管理&#x27;</span>, <span class="hljs-attr">icon</span>: <span class="hljs-string">&#x27;smile&#x27;</span>, <span class="hljs-attr">component</span>: <span class="hljs-string">&#x27;./Admin/UserManage&#x27;</span> &#125;,<br>    &#123; <span class="hljs-attr">path</span>: <span class="hljs-string">&#x27;/admin/sub-page&#x27;</span>, <span class="hljs-attr">name</span>: <span class="hljs-string">&#x27;二级管理页&#x27;</span>, <span class="hljs-attr">icon</span>: <span class="hljs-string">&#x27;smile&#x27;</span>, <span class="hljs-attr">component</span>: <span class="hljs-string">&#x27;./Welcome&#x27;</span> &#125;,<br>    &#123; <span class="hljs-attr">component</span>: <span class="hljs-string">&#x27;./404&#x27;</span> &#125;,<br>  ],<br>&#125;,<br></code></pre></td></tr></table></figure>

<ul>
<li>访问<a target="_blank" rel="noopener" href="http://localhost:8000/admin/user-manager%E5%8F%91%E7%8E%B0%E6%97%A0%E6%9D%83%E8%AE%BF%E9%97%AE">http://localhost:8000/admin/user-manager发现无权访问</a>    好像存在访问权限    访问不到</li>
</ul>
<h4 id="项目全局入口"><a href="#项目全局入口" class="headerlink" title="项目全局入口"></a>项目全局入口</h4><h6 id="app-tsx是项目全局入口-里面包含了访问页面时-就会调用的方法-重定向到Login页-查询用户登录态"><a href="#app-tsx是项目全局入口-里面包含了访问页面时-就会调用的方法-重定向到Login页-查询用户登录态" class="headerlink" title="app.tsx是项目全局入口     里面包含了访问页面时, 就会调用的方法,     重定向到Login页   查询用户登录态"></a>app.tsx是项目全局入口     里面包含了访问页面时, 就会调用的方法,     <strong>重定向到Login页   查询用户登录态</strong></h6><figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><code class="hljs typescript">&#123;<br>  <span class="hljs-attr">path</span>: <span class="hljs-string">&#x27;/admin&#x27;</span>,<br>  <span class="hljs-attr">name</span>: <span class="hljs-string">&#x27;管理页&#x27;</span>,<br>  <span class="hljs-attr">icon</span>: <span class="hljs-string">&#x27;crown&#x27;</span>,<br>  <span class="hljs-comment">// 管理员权限校验</span><br>  <span class="hljs-attr">access</span>: <span class="hljs-string">&#x27;canAdmin&#x27;</span>,<br>  <span class="hljs-attr">component</span>: <span class="hljs-string">&#x27;./Admin&#x27;</span>,<br>  <span class="hljs-attr">routes</span>: [<br>    &#123; <span class="hljs-attr">path</span>: <span class="hljs-string">&#x27;/admin/user-manager&#x27;</span>, <span class="hljs-attr">name</span>: <span class="hljs-string">&#x27;用户管理&#x27;</span>, <span class="hljs-attr">icon</span>: <span class="hljs-string">&#x27;smile&#x27;</span>, <span class="hljs-attr">component</span>: <span class="hljs-string">&#x27;./Admin/UserManage&#x27;</span> &#125;,<br>    &#123; <span class="hljs-attr">path</span>: <span class="hljs-string">&#x27;/admin/sub-page&#x27;</span>, <span class="hljs-attr">name</span>: <span class="hljs-string">&#x27;二级管理页&#x27;</span>, <span class="hljs-attr">icon</span>: <span class="hljs-string">&#x27;smile&#x27;</span>, <span class="hljs-attr">component</span>: <span class="hljs-string">&#x27;./Welcome&#x27;</span> &#125;,<br>    &#123; <span class="hljs-attr">component</span>: <span class="hljs-string">&#x27;./404&#x27;</span> &#125;,<br>  ],<br>&#125;,<br></code></pre></td></tr></table></figure>

<h6 id="看到那个access了吗-通过校验’canAdmin’的真假-判断是否具有管理员权限-这就是控制了这个路由的访问权限-怎么实现的"><a href="#看到那个access了吗-通过校验’canAdmin’的真假-判断是否具有管理员权限-这就是控制了这个路由的访问权限-怎么实现的" class="headerlink" title="看到那个access了吗  通过校验’canAdmin’的真假   判断是否具有管理员权限  这就是控制了这个路由的访问权限    怎么实现的?"></a>看到那个access了吗  通过校验’canAdmin’的真假   判断是否具有管理员权限  这就是控制了这个路由的访问权限    怎么实现的?</h6><h4 id="访问权限管理"><a href="#访问权限管理" class="headerlink" title="访问权限管理"></a>访问权限管理</h4><h6 id="access-ts是访问权限管理-在查询到用户登录态后-通过返回结果CurrentUser来校验-这段逻辑非常简单-我们可以修改为自己的逻辑"><a href="#access-ts是访问权限管理-在查询到用户登录态后-通过返回结果CurrentUser来校验-这段逻辑非常简单-我们可以修改为自己的逻辑" class="headerlink" title="access.ts是访问权限管理    在查询到用户登录态后    通过返回结果CurrentUser来校验    这段逻辑非常简单    我们可以修改为自己的逻辑"></a>access.ts是访问权限管理    在查询到用户登录态后    通过返回结果CurrentUser来校验    这段逻辑非常简单    我们可以修改为自己的逻辑</h6><figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs typescript"><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-keyword">function</span> <span class="hljs-title function_">access</span>(<span class="hljs-params">initialState: &#123; currentUser?: API.CurrentUser &#125; | <span class="hljs-literal">undefined</span></span>) &#123;<br>  <span class="hljs-keyword">const</span> &#123; currentUser &#125; = initialState ?? &#123;&#125;;<br>  <span class="hljs-comment">// 权限校验</span><br>  <span class="hljs-keyword">return</span> &#123;<br>    <span class="hljs-comment">// 校验管理员权限</span><br>    <span class="hljs-attr">canAdmin</span>: currentUser &amp;&amp; currentUser.<span class="hljs-property">access</span> === <span class="hljs-string">&#x27;admin&#x27;</span><br>  &#125;;<br>&#125;<br></code></pre></td></tr></table></figure>

<ul>
<li>修改访问路由的管理员权限的校验规则</li>
</ul>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs typescript"><span class="hljs-comment">// 校验管理员权限</span><br><span class="hljs-attr">canAdmin</span>: currentUser &amp;&amp; currentUser.<span class="hljs-property">userRole</span> === <span class="hljs-number">1</span>,<br></code></pre></td></tr></table></figure>

<ul>
<li>找一个管理员账号登录,  发现<a target="_blank" rel="noopener" href="http://localhost:8000/admin/user-manager%E9%A1%B5%E9%9D%A2%E5%8F%AF%E4%BB%A5%E8%AE%BF%E9%97%AE%E4%BA%86">http://localhost:8000/admin/user-manager页面可以访问了</a>  因为我们由管理员权限了</li>
</ul>
<h2 id="正确显示管理页面"><a href="#正确显示管理页面" class="headerlink" title="正确显示管理页面"></a>正确显示管理页面</h2><h6 id="原本的页面显示组件是-Admin-tsx"><a href="#原本的页面显示组件是-Admin-tsx" class="headerlink" title="原本的页面显示组件是 Admin.tsx"></a>原本的页面显示组件是 Admin.tsx</h6><figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs typescript"><span class="hljs-comment">// 父路由</span><br><span class="hljs-attr">path</span>: <span class="hljs-string">&#x27;/admin&#x27;</span>,<br><span class="hljs-attr">name</span>: <span class="hljs-string">&#x27;管理页&#x27;</span>,<br><span class="hljs-attr">icon</span>: <span class="hljs-string">&#x27;crown&#x27;</span>,<br><span class="hljs-attr">component</span>: <span class="hljs-string">&#x27;./Admin&#x27;</span>,<br>	<span class="hljs-comment">// 子路由</span><br>    <span class="hljs-attr">routes</span>: [<br>      &#123; <span class="hljs-attr">path</span>: <span class="hljs-string">&#x27;/admin/user-manager&#x27;</span>, <span class="hljs-attr">name</span>: <span class="hljs-string">&#x27;用户管理&#x27;</span>, <span class="hljs-attr">icon</span>: <span class="hljs-string">&#x27;smile&#x27;</span>, <span class="hljs-attr">component</span>: <span class="hljs-string">&#x27;./Admin/UserManage&#x27;</span> &#125;,<br>      &#123; <span class="hljs-attr">component</span>: <span class="hljs-string">&#x27;./404&#x27;</span> &#125;,<br>    ],<br></code></pre></td></tr></table></figure>

<h6 id="组件里面这么写"><a href="#组件里面这么写" class="headerlink" title="组件里面这么写:"></a>组件里面这么写:</h6><figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs typescript">onst <span class="hljs-title class_">Admin</span>: <span class="hljs-title class_">React</span>.<span class="hljs-property">FC</span> = <span class="hljs-function">(<span class="hljs-params">&#123;children&#125;</span>) =&gt;</span> &#123;<br>  <span class="hljs-keyword">return</span> (<br>    <span class="language-xml"><span class="hljs-tag">&lt;<span class="hljs-name">PageHeaderWrapper</span> <span class="hljs-attr">content</span>=<span class="hljs-string">&#123;</span>&#x27; <span class="hljs-attr">这个页面只有</span> <span class="hljs-attr">admin</span> <span class="hljs-attr">权限才能查看</span>&#x27;&#125;&gt;</span></span><br><span class="language-xml">    // 展示子路由组件</span><br><span class="language-xml">      &#123;children&#125;</span><br><span class="language-xml">    <span class="hljs-tag">&lt;/<span class="hljs-name">PageHeaderWrapper</span>&gt;</span></span><br>  );<br>&#125;;<br></code></pre></td></tr></table></figure>

<h6 id="这样管理页面就能显示我们定义的组件-Admin-UserManage-index-tsx-了"><a href="#这样管理页面就能显示我们定义的组件-Admin-UserManage-index-tsx-了" class="headerlink" title="这样管理页面就能显示我们定义的组件 Admin/UserManage/index.tsx 了"></a>这样管理页面就能显示我们定义的组件 Admin/UserManage/index.tsx 了</h6><ul>
<li><p>我们上ProComponents的高级表格里找一个高级表格, 作为管理页面</p>
</li>
<li><p>直接找一个漂亮有用的, 粘贴到/UserManage/index.tsx里</p>
</li>
<li><p>接下来就是对该页面的改造了</p>
</li>
</ul>
<h2 id="改造新的组件-管理页面"><a href="#改造新的组件-管理页面" class="headerlink" title="改造新的组件(管理页面)"></a>改造新的组件(管理页面)</h2><h4 id="改造表格数据-数据如何展示"><a href="#改造表格数据-数据如何展示" class="headerlink" title="改造表格数据(数据如何展示)"></a>改造表格数据(数据如何展示)</h4><ul>
<li>改造返回数据类型 (API.CurrentUser) 和各列名</li>
</ul>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br></pre></td><td class="code"><pre><code class="hljs typescript"><span class="hljs-comment">// 返回的数据类型改造为Api.currentUser</span><br><span class="hljs-keyword">const</span> <span class="hljs-attr">columns</span>: <span class="hljs-title class_">ProColumns</span>&lt;<span class="hljs-variable constant_">API</span>.<span class="hljs-property">CurrentUser</span>&gt;[] = [<br>  &#123;<br>    <span class="hljs-attr">title</span>: <span class="hljs-string">&#x27;账号&#x27;</span>,<br>    <span class="hljs-attr">dataIndex</span>: <span class="hljs-string">&#x27;userAccount&#x27;</span>,<br>    <span class="hljs-attr">copyable</span>: <span class="hljs-literal">true</span>,<br>  &#125;,<br>  &#123;<br>    <span class="hljs-attr">title</span>: <span class="hljs-string">&#x27;昵称&#x27;</span>,<br>    <span class="hljs-attr">dataIndex</span>: <span class="hljs-string">&#x27;username&#x27;</span>,<br>    <span class="hljs-attr">copyable</span>: <span class="hljs-literal">true</span>,<br>  &#125;,<br>  &#123;<br>    <span class="hljs-attr">title</span>: <span class="hljs-string">&#x27;头像&#x27;</span>,<br>    <span class="hljs-attr">dataIndex</span>: <span class="hljs-string">&#x27;avatarUrl&#x27;</span>,<br>    <span class="hljs-attr">copyable</span>: <span class="hljs-literal">true</span>,<br>  &#125;,<br>  &#123;<br>    <span class="hljs-attr">title</span>: <span class="hljs-string">&#x27;性别&#x27;</span>,<br>    <span class="hljs-attr">dataIndex</span>: <span class="hljs-string">&#x27;gender&#x27;</span>,<br>    <span class="hljs-attr">copyable</span>: <span class="hljs-literal">true</span>,<br>  &#125;,<br>  &#123;<br>    <span class="hljs-attr">title</span>: <span class="hljs-string">&#x27;电话&#x27;</span>,<br>    <span class="hljs-attr">dataIndex</span>: <span class="hljs-string">&#x27;phone&#x27;</span>,<br>    <span class="hljs-attr">copyable</span>: <span class="hljs-literal">true</span>,<br>  &#125;,<br>  &#123;<br>    <span class="hljs-attr">title</span>: <span class="hljs-string">&#x27;邮件&#x27;</span>,<br>    <span class="hljs-attr">dataIndex</span>: <span class="hljs-string">&#x27;email&#x27;</span>,<br>    <span class="hljs-attr">copyable</span>: <span class="hljs-literal">true</span>,<br>  &#125;,<br>  &#123;<br>    <span class="hljs-attr">title</span>: <span class="hljs-string">&#x27;角色&#x27;</span>,<br>    <span class="hljs-attr">dataIndex</span>: <span class="hljs-string">&#x27;userRole&#x27;</span>,<br>    <span class="hljs-attr">copyable</span>: <span class="hljs-literal">true</span>,<br>  &#125;,<br>  &#123;<br>    <span class="hljs-attr">title</span>: <span class="hljs-string">&#x27;创建时间&#x27;</span>,<br>    <span class="hljs-attr">dataIndex</span>: <span class="hljs-string">&#x27;createTime&#x27;</span>,<br>    <span class="hljs-attr">copyable</span>: <span class="hljs-literal">true</span>,<br>  &#125;,<br>  &#123;<br>    <span class="hljs-attr">disable</span>: <span class="hljs-literal">true</span>,<br>    <span class="hljs-attr">title</span>: <span class="hljs-string">&#x27;状态&#x27;</span>,<br>    <span class="hljs-attr">dataIndex</span>: <span class="hljs-string">&#x27;state&#x27;</span>,<br>    <span class="hljs-attr">filters</span>: <span class="hljs-literal">true</span>,<br>    <span class="hljs-attr">onFilter</span>: <span class="hljs-literal">true</span>,<br>    <span class="hljs-attr">ellipsis</span>: <span class="hljs-literal">true</span>,<br>    <span class="hljs-attr">valueType</span>: <span class="hljs-string">&#x27;select&#x27;</span>,<br>    <span class="hljs-attr">valueEnum</span>: &#123;<br>      <span class="hljs-attr">all</span>: &#123;<span class="hljs-attr">text</span>: <span class="hljs-string">&#x27;超长&#x27;</span>.<span class="hljs-title function_">repeat</span>(<span class="hljs-number">50</span>)&#125;,<br>      <span class="hljs-attr">open</span>: &#123;<br>        <span class="hljs-attr">text</span>: <span class="hljs-string">&#x27;未解决&#x27;</span>,<br>        <span class="hljs-attr">status</span>: <span class="hljs-string">&#x27;Error&#x27;</span>,<br>      &#125;,<br>      <span class="hljs-attr">closed</span>: &#123;<br>        <span class="hljs-attr">text</span>: <span class="hljs-string">&#x27;已解决&#x27;</span>,<br>        <span class="hljs-attr">status</span>: <span class="hljs-string">&#x27;Success&#x27;</span>,<br>        <span class="hljs-attr">disabled</span>: <span class="hljs-literal">true</span>,<br>      &#125;,<br>      <span class="hljs-attr">processing</span>: &#123;<br>        <span class="hljs-attr">text</span>: <span class="hljs-string">&#x27;解决中&#x27;</span>,<br>        <span class="hljs-attr">status</span>: <span class="hljs-string">&#x27;Processing&#x27;</span>,<br>      &#125;,<br>    &#125;,<br>  &#125;,<br><br>];<br></code></pre></td></tr></table></figure>

<h4 id="改造访问路径-数据从何而来"><a href="#改造访问路径-数据从何而来" class="headerlink" title="改造访问路径(数据从何而来)"></a>改造访问路径(数据从何而来)</h4><figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><code class="hljs typescript">columns=&#123;columns&#125;<br>actionRef=&#123;actionRef&#125;<br>cardBordered<br>request=&#123;<span class="hljs-keyword">async</span> (params = &#123;&#125;, sort, filter) =&gt; &#123;<br>  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(sort, filter);<br>  <span class="hljs-comment">// 返回userList</span><br>  <span class="hljs-comment">// 自定义函数</span><br>  <span class="hljs-keyword">const</span> userList = <span class="hljs-keyword">await</span> <span class="hljs-title function_">searchUsers</span>();<br>  <span class="hljs-keyword">return</span> &#123;<br>    <span class="hljs-attr">data</span>: userList<br>  &#125;<br>&#125;&#125;<br></code></pre></td></tr></table></figure>

<ul>
<li>在api.ts下编写自定义函数searchUsers, 并设置访问路径</li>
</ul>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs typescript"><span class="hljs-comment">/** 此处后端没有提供注释 GET /api/notices */</span><br><span class="hljs-keyword">export</span> <span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title function_">searchUsers</span>(<span class="hljs-params">options?: &#123; [key: <span class="hljs-built_in">string</span>]: <span class="hljs-built_in">any</span> &#125;</span>) &#123;<br><span class="hljs-comment">// 返回数据格式为API.CurrentUser</span><br>  <span class="hljs-keyword">return</span> request&lt;<span class="hljs-variable constant_">API</span>.<span class="hljs-property">CurrentUser</span>&gt;(<span class="hljs-string">&#x27;/api/user/search&#x27;</span>, &#123;<br>    <span class="hljs-attr">method</span>: <span class="hljs-string">&#x27;GET&#x27;</span>,<br>    ...(options || &#123;&#125;),<br>  &#125;);<br>&#125;<br></code></pre></td></tr></table></figure>

<ul>
<li>后端返回所有用户数据, 并展示在表格中 展示成功了</li>
</ul>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><code class="hljs typescript"><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * 查询用户</span><br><span class="hljs-comment"> *</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@param</span> username 用户名</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@return</span> 查到的用户</span><br><span class="hljs-comment"> */</span><br><span class="hljs-meta">@GetMapping</span>(<span class="hljs-string">&quot;/search&quot;</span>)<br><span class="hljs-keyword">public</span> <span class="hljs-title class_">List</span>&lt;<span class="hljs-title class_">User</span>&gt; <span class="hljs-title function_">userSearch</span>(<span class="hljs-params"><span class="hljs-built_in">String</span> username, HttpServletRequest request</span>) &#123;<br>    <span class="hljs-comment">// 1.校验权限</span><br>    <span class="hljs-keyword">if</span> (!<span class="hljs-title function_">isAdmin</span>(request))<br>        <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayList</span>&lt;&gt;();<br>    <span class="hljs-comment">// 2.判空, 默认查询全部</span><br>    <span class="hljs-title class_">QueryWrapper</span>&lt;<span class="hljs-title class_">User</span>&gt; qw = <span class="hljs-keyword">new</span> <span class="hljs-title class_">QueryWrapper</span>&lt;&gt;();<br>    <span class="hljs-keyword">if</span> (<span class="hljs-title class_">StringUtils</span>.<span class="hljs-title function_">isNotBlank</span>(username))<br>        qw.<span class="hljs-title function_">like</span>(<span class="hljs-string">&quot;username&quot;</span>, username);<br>    <span class="hljs-comment">// 3.查询</span><br>    <span class="hljs-title class_">List</span>&lt;<span class="hljs-title class_">User</span>&gt; userList = userService.<span class="hljs-title function_">list</span>(qw);<br>    <span class="hljs-comment">// 4.返回脱敏的用户信息</span><br>    <span class="hljs-keyword">return</span> userList.<span class="hljs-title function_">stream</span>().<span class="hljs-title function_">map</span>(user -&gt; userService.<span class="hljs-title function_">getSafetyUser</span>(user)).<span class="hljs-title function_">collect</span>(<span class="hljs-title class_">Collectors</span>.<span class="hljs-title function_">toList</span>());<br>&#125;<br></code></pre></td></tr></table></figure>

<h6 id="项目全局命名空间-把一组TS类型全部定义到了这个命名空间下-即定义了一组返回数据对象-取的时候就不需要import了-直接API-TS类型就可以取到"><a href="#项目全局命名空间-把一组TS类型全部定义到了这个命名空间下-即定义了一组返回数据对象-取的时候就不需要import了-直接API-TS类型就可以取到" class="headerlink" title="项目全局命名空间, 把一组TS类型全部定义到了这个命名空间下, 即定义了一组返回数据对象, 取的时候就不需要import了, 直接API.TS类型就可以取到"></a><em><strong>项目全局命名空间</strong></em>, <em>把一组TS类型全部定义到了这个命名空间下, 即定义了一组返回数据对象, 取的时候就不需要import了, 直接API.TS类型就可以取到</em></h6><h6 id="src-services-ant-design-pro-typings-d-ts"><a href="#src-services-ant-design-pro-typings-d-ts" class="headerlink" title="src/services/ant-design-pro/typings.d.ts"></a>src/services/ant-design-pro/typings.d.ts</h6><figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><code class="hljs typescript"><span class="hljs-keyword">declare</span> <span class="hljs-keyword">namespace</span> API &#123;<br>  <span class="hljs-keyword">type</span> <span class="hljs-title class_">CurrentUser</span> = &#123;<br>  .............<br>  &#125;;<br><br>  <span class="hljs-keyword">type</span> <span class="hljs-title class_">LoginResult</span> = &#123;<br>   .............<br>  &#125;;<br><br>  <span class="hljs-keyword">type</span> <span class="hljs-title class_">RegisterResult</span> = <span class="hljs-built_in">number</span>;<br><br>  <span class="hljs-keyword">type</span> <span class="hljs-title class_">PageParams</span> = &#123;<br>   .............<br>  &#125;;<br><br>  <span class="hljs-keyword">type</span> <span class="hljs-title class_">RuleListItem</span> = &#123;<br>   .............<br>  &#125;;<br><br>  <span class="hljs-keyword">type</span> <span class="hljs-title class_">RuleList</span> = &#123;<br>   .............<br>  &#125;;<br><br>  <span class="hljs-keyword">type</span> <span class="hljs-title class_">FakeCaptcha</span> = &#123;<br>   .............<br>  &#125;;<br><br>  <span class="hljs-keyword">type</span> <span class="hljs-title class_">LoginParams</span> = &#123;<br>   .............<br>  &#125;;<br><br>  <span class="hljs-keyword">type</span> <span class="hljs-title class_">RegisterParams</span> = &#123;<br>   .............<br>  &#125;;<br><br>  <span class="hljs-keyword">type</span> <span class="hljs-title class_">ErrorResponse</span> = &#123;<br>   .............<br>  &#125;;<br><br>  <span class="hljs-keyword">type</span> <span class="hljs-title class_">NoticeIconList</span> = &#123;<br>   .............<br>  &#125;;<br><br>  <span class="hljs-keyword">type</span> <span class="hljs-title class_">NoticeIconItemType</span> = <span class="hljs-string">&#x27;notification&#x27;</span> | <span class="hljs-string">&#x27;message&#x27;</span> | <span class="hljs-string">&#x27;event&#x27;</span>;<br><br>  <span class="hljs-keyword">type</span> <span class="hljs-title class_">NoticeIconItem</span> = &#123;<br>   .............<br>  &#125;;<br>&#125;<br></code></pre></td></tr></table></figure>

<h6 id="src-services-ant-design-pro-api-ts-这里定义了许多请求接口-根据请求地址-请求方式发出请求"><a href="#src-services-ant-design-pro-api-ts-这里定义了许多请求接口-根据请求地址-请求方式发出请求" class="headerlink" title="src/services/ant-design-pro/api.ts    这里定义了许多请求接口    根据请求地址 请求方式发出请求"></a>src/services/ant-design-pro/api.ts    <em>这里定义了许多请求接口</em>    <em>根据请求地址</em> 请求方式发出请求</h6><figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs typescript"><span class="hljs-keyword">import</span> &#123;request&#125; <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;umi&#x27;</span>;<br></code></pre></td></tr></table></figure>

<h2 id="修改表格显示细节"><a href="#修改表格显示细节" class="headerlink" title="修改表格显示细节"></a>修改表格显示细节</h2><ul>
<li><p>通过columns定义表格有哪些列</p>
</li>
<li><p>column属性</p>
</li>
</ul>
<p>​        <em><strong>dataIndex    对应返回数据对象的属性</strong></em></p>
<p>​        <em><strong>title    表格列名</strong></em></p>
<p>​        <em><strong>copyable    是否允许复制</strong></em></p>
<p>​        <em><strong>ellipsis    是否允许缩略</strong></em></p>
<p>​        <em><strong>valueType    用于声明这一列的类型</strong></em></p>
<ul>
<li>头像</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs vue">&#123;<br>  title: &#x27;头像&#x27;,<br>  dataIndex: &#x27;avatarUrl&#x27;,<br>  copyable: true,<br>  render: (_, record) =&gt; (<br>    &lt;div&gt;<br>      &lt;img src=&#123;record.avatarUrl&#125; width=&#123;100&#125;/&gt;<br>    &lt;/div&gt;<br>  )<br>&#125;,<br></code></pre></td></tr></table></figure>

<ul>
<li>性别    角色    状态</li>
</ul>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><code class="hljs typescript">&#123;<br>  <span class="hljs-attr">title</span>: <span class="hljs-string">&#x27;性别&#x27;</span>,<br>  <span class="hljs-attr">dataIndex</span>: <span class="hljs-string">&#x27;gender&#x27;</span>,<br>  <span class="hljs-attr">valueType</span>: <span class="hljs-string">&#x27;select&#x27;</span>,<br>  <span class="hljs-attr">valueEnum</span>: &#123;<br>    <span class="hljs-number">0</span>: &#123;<br>      <span class="hljs-attr">text</span>: <span class="hljs-string">&#x27;女&#x27;</span>,<br>    &#125;,<br>    <span class="hljs-number">1</span>: &#123;<br>      <span class="hljs-attr">text</span>: <span class="hljs-string">&#x27;男&#x27;</span>,<br>    &#125;,<br>  &#125;,<br>&#125;,<br></code></pre></td></tr></table></figure>

<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><code class="hljs typescript">&#123;<br>  <span class="hljs-attr">title</span>: <span class="hljs-string">&#x27;角色&#x27;</span>,<br>  <span class="hljs-attr">dataIndex</span>: <span class="hljs-string">&#x27;userRole&#x27;</span>,<br>  <span class="hljs-attr">valueType</span>: <span class="hljs-string">&#x27;select&#x27;</span>,<br>  <span class="hljs-attr">valueEnum</span>: &#123;<br>    <span class="hljs-number">0</span>: &#123;<br>      <span class="hljs-attr">text</span>: <span class="hljs-string">&#x27;普通用户&#x27;</span>,<br>      <span class="hljs-attr">status</span>: <span class="hljs-string">&#x27;Default&#x27;</span>,<br>    &#125;,<br>    <span class="hljs-number">1</span>: &#123;<br>      <span class="hljs-attr">text</span>: <span class="hljs-string">&#x27;管理员&#x27;</span>,<br>      <span class="hljs-attr">status</span>: <span class="hljs-string">&#x27;Success&#x27;</span>,<br>    &#125;,<br>  &#125;,<br>&#125;,<br></code></pre></td></tr></table></figure>

<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><code class="hljs typescript">&#123;<br>  <span class="hljs-attr">title</span>: <span class="hljs-string">&#x27;状态&#x27;</span>,<br>  <span class="hljs-attr">dataIndex</span>: <span class="hljs-string">&#x27;userStatus&#x27;</span>,<br>  <span class="hljs-attr">valueType</span>: <span class="hljs-string">&#x27;select&#x27;</span>,<br>  <span class="hljs-attr">valueEnum</span>: &#123;<br>    <span class="hljs-number">0</span>: &#123;<br>      <span class="hljs-attr">text</span>: <span class="hljs-string">&#x27;正常&#x27;</span>,<br>      <span class="hljs-attr">status</span>: <span class="hljs-string">&#x27;Success&#x27;</span>,<br>    &#125;,<br>    <span class="hljs-number">1</span>: &#123;<br>      <span class="hljs-attr">text</span>: <span class="hljs-string">&#x27;异常&#x27;</span>,<br>      <span class="hljs-attr">status</span>: <span class="hljs-string">&#x27;Error&#x27;</span>,<br>    &#125;,<br>  &#125;,<br>&#125;,<br></code></pre></td></tr></table></figure>

<h1 id="代码优化"><a href="#代码优化" class="headerlink" title="代码优化"></a>代码优化</h1><h2 id="新增注销功能"><a href="#新增注销功能" class="headerlink" title="新增注销功能"></a>新增注销功能</h2><ul>
<li>service层新增userLogout</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-type">int</span> <span class="hljs-title function_">userLogout</span><span class="hljs-params">(HttpServletRequest request)</span>;<br></code></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">    * 用户注销</span><br><span class="hljs-comment">    *</span><br><span class="hljs-comment">    * <span class="hljs-doctag">@param</span> request</span><br><span class="hljs-comment">    * <span class="hljs-doctag">@return</span></span><br><span class="hljs-comment">    */</span><br>   <span class="hljs-meta">@Override</span><br>   <span class="hljs-keyword">public</span> <span class="hljs-type">int</span> <span class="hljs-title function_">userLogout</span><span class="hljs-params">(HttpServletRequest request)</span> &#123;<br>   <span class="hljs-comment">// 移除session</span><br>       request.getSession().removeAttribute(USER_LOGIN_STATE);<br>       <span class="hljs-keyword">return</span> <span class="hljs-number">1</span>;<br>   &#125;<br></code></pre></td></tr></table></figure>

<ul>
<li>controller层新增userLogout</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">   * 用户登录</span><br><span class="hljs-comment">   *</span><br><span class="hljs-comment">   * <span class="hljs-doctag">@param</span> request request</span><br><span class="hljs-comment">   * <span class="hljs-doctag">@return</span> int</span><br><span class="hljs-comment">   */</span><br>  <span class="hljs-meta">@PostMapping(&quot;/logout&quot;)</span><br>  <span class="hljs-keyword">public</span> Integer <span class="hljs-title function_">userLogout</span><span class="hljs-params">(HttpServletRequest request)</span> &#123;<br>      <span class="hljs-comment">//controller对参数的校验</span><br>      <span class="hljs-keyword">if</span> (request == <span class="hljs-literal">null</span>)<br>          <span class="hljs-keyword">return</span> <span class="hljs-literal">null</span>;<br><br>      <span class="hljs-keyword">return</span> userService.userLogout(request);<br>  &#125;<br></code></pre></td></tr></table></figure>

<ul>
<li>前端src/RightContent/AvatarDropdown.tsx下有注销功能</li>
</ul>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><code class="hljs typescript"><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * 退出登录，并且将当前的 url 保存</span><br><span class="hljs-comment"> */</span><br><span class="hljs-keyword">const</span> <span class="hljs-title function_">loginOut</span> = <span class="hljs-keyword">async</span> (<span class="hljs-params"></span>) =&gt; &#123;<br>  <span class="hljs-keyword">await</span> <span class="hljs-title function_">outLogin</span>();<br>  <span class="hljs-keyword">const</span> &#123;query = &#123;&#125;, search, pathname&#125; = history.<span class="hljs-property">location</span>;<br>  <span class="hljs-keyword">const</span> &#123;redirect&#125; = query;<br>  <span class="hljs-comment">// Note: There may be security issues, please note</span><br>  <span class="hljs-keyword">if</span> (<span class="hljs-variable language_">window</span>.<span class="hljs-property">location</span>.<span class="hljs-property">pathname</span> !== <span class="hljs-string">&#x27;/user/login&#x27;</span> &amp;&amp; !redirect) &#123;<br>    history.<span class="hljs-title function_">replace</span>(&#123;<br>      <span class="hljs-attr">pathname</span>: <span class="hljs-string">&#x27;/user/login&#x27;</span>,<br>      <span class="hljs-attr">search</span>: <span class="hljs-title function_">stringify</span>(&#123;<br>        <span class="hljs-attr">redirect</span>: pathname + search,<br>      &#125;),<br>    &#125;);<br>  &#125;<br>&#125;;<br></code></pre></td></tr></table></figure>

<ul>
<li>修改注销接口 请求路径 </li>
</ul>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs typescript"><span class="hljs-comment">/** 退出登录接口 POST /api/user/logout */</span><br><span class="hljs-keyword">export</span> <span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title function_">outLogin</span>(<span class="hljs-params">options?: &#123; [key: <span class="hljs-built_in">string</span>]: <span class="hljs-built_in">any</span> &#125;</span>) &#123;<br>  <span class="hljs-keyword">return</span> request&lt;<span class="hljs-title class_">Record</span>&lt;<span class="hljs-built_in">string</span>, <span class="hljs-built_in">any</span>&gt;&gt;(<span class="hljs-string">&#x27;/api/user/logout&#x27;</span>, &#123;<br>    <span class="hljs-attr">method</span>: <span class="hljs-string">&#x27;POST&#x27;</span>,<br>    ...(options || &#123;&#125;),<br>  &#125;);<br>&#125;<br></code></pre></td></tr></table></figure>

<h4 id="注销功能优化完毕"><a href="#注销功能优化完毕" class="headerlink" title="注销功能优化完毕"></a>注销功能优化完毕</h4><h2 id="用户必填信息新增星球编号"><a href="#用户必填信息新增星球编号" class="headerlink" title="用户必填信息新增星球编号"></a>用户必填信息新增星球编号</h2><ul>
<li>user表新增字段planet_code</li>
<li>重新生成对应实体类 domain    Mapper.xml  </li>
<li>注册接收类新增planetCode</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@Data</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">UserRegisterRequest</span> &#123;<br>    <span class="hljs-keyword">private</span> String userAccount;<br>    <span class="hljs-keyword">private</span> String userPassword;<br>    <span class="hljs-keyword">private</span> String checkPassword;<br>    <span class="hljs-keyword">private</span> String planetCode;<br>&#125;<br></code></pre></td></tr></table></figure>

<p>service层</p>
<ul>
<li>注册校验新增</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-type">long</span> <span class="hljs-title function_">userRegister</span><span class="hljs-params">(String userAccount, String userPassword, String checkPassword, String planetCode)</span>;<br></code></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">// 1.6.星球编号不能超过5位</span><br>       <span class="hljs-keyword">if</span> (planetCode.length() &gt; <span class="hljs-number">5</span>)<br>           <span class="hljs-keyword">return</span> -<span class="hljs-number">1</span>;<br></code></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">// 1.8.星球编号不能重复</span><br>       QueryWrapper&lt;User&gt; pc_lqw = <span class="hljs-keyword">new</span> <span class="hljs-title class_">QueryWrapper</span>&lt;&gt;();<br>       pc_lqw.eq(<span class="hljs-string">&quot;planet_code&quot;</span>, planetCode);<br>       <span class="hljs-type">Long</span> <span class="hljs-variable">pc_count</span> <span class="hljs-operator">=</span> userMapper.selectCount(pc_lqw);<br>       <span class="hljs-keyword">if</span> (pc_count &gt; <span class="hljs-number">0</span>)<br>           <span class="hljs-keyword">return</span> -<span class="hljs-number">1</span>;<br></code></pre></td></tr></table></figure>

<ul>
<li>用户信息脱敏新增</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs java">safetyUser.setPlanetCode(originUser.getPlanetCode());<br></code></pre></td></tr></table></figure>

<p>controller层</p>
<ul>
<li>controller参数校验</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">//controller对参数的校验</span><br><span class="hljs-keyword">if</span> (StringUtils.isAnyBlank(userAccount, userPassword, checkPassword, planetCode))<br>    <span class="hljs-keyword">return</span> <span class="hljs-literal">null</span>;<br></code></pre></td></tr></table></figure>

<ul>
<li>注册页面新增星球编号填写和校验</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><code class="hljs vue">&lt;ProFormText<br>  name=&quot;planetCode&quot;<br>  fieldProps=&#123;&#123;<br>    size: &#x27;large&#x27;,<br>    prefix: &lt;LockOutlined className=&#123;styles.prefixIcon&#125;/&gt;,<br>  &#125;&#125;<br>  placeholder=&#123;&#x27;请输入星球编号&#x27;&#125;<br>  rules=&#123;[<br>    &#123;<br>      required: true,<br>      message: &#x27;星球编号是必填项！&#x27;,<br>    &#125;,<br>    &#123;<br>      max: 5,<br>      type: &#x27;string&#x27;,<br>      message: &#x27;长度不能大于5位&#x27;,<br>    &#125;,<br>  ]&#125;<br>/&gt;<br></code></pre></td></tr></table></figure>

<ul>
<li>测试类新增planetCode</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@Test</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">userRegister</span><span class="hljs-params">()</span> &#123;<br>    <span class="hljs-type">String</span> <span class="hljs-variable">userAccount</span> <span class="hljs-operator">=</span> <span class="hljs-string">&quot;memory&quot;</span>;<br>    <span class="hljs-type">String</span> <span class="hljs-variable">userPassword</span> <span class="hljs-operator">=</span> <span class="hljs-string">&quot;&quot;</span>;<br>    <span class="hljs-type">String</span> <span class="hljs-variable">checkPassword</span> <span class="hljs-operator">=</span> <span class="hljs-string">&quot;123456&quot;</span>;<br>    <span class="hljs-type">String</span> <span class="hljs-variable">planetCode</span> <span class="hljs-operator">=</span> <span class="hljs-string">&quot;17265&quot;</span>;<br><br>    <span class="hljs-type">long</span> <span class="hljs-variable">result</span> <span class="hljs-operator">=</span> userService.userRegister(userAccount, userPassword, checkPassword, planetCode);<br>    Assertions.assertEquals(-<span class="hljs-number">1</span>, result);<br>    userAccount = <span class="hljs-string">&quot;mem&quot;</span>;<br>    result = userService.userRegister(userAccount, userPassword, checkPassword, planetCode);<br>    Assertions.assertEquals(-<span class="hljs-number">1</span>, result);<br>    userPassword = <span class="hljs-string">&quot;123456&quot;</span>;<br>    result = userService.userRegister(userAccount, userPassword, checkPassword, planetCode);<br>    Assertions.assertEquals(-<span class="hljs-number">1</span>, result);<br>    userAccount = <span class="hljs-string">&quot;me mory&quot;</span>;<br>    userPassword = <span class="hljs-string">&quot;12345678&quot;</span>;<br>    result = userService.userRegister(userAccount, userPassword, checkPassword, planetCode);<br>    Assertions.assertEquals(-<span class="hljs-number">1</span>, result);<br>    userAccount = <span class="hljs-string">&quot;me mory&quot;</span>;<br>    checkPassword = <span class="hljs-string">&quot;12345678&quot;</span>;<br>    result = userService.userRegister(userAccount, userPassword, checkPassword, planetCode);<br>    Assertions.assertEquals(-<span class="hljs-number">1</span>, result);<br>    userAccount = <span class="hljs-string">&quot;memory4&quot;</span>;<br>    result = userService.userRegister(userAccount, userPassword, checkPassword, planetCode);<br>    Assertions.assertTrue(result &gt; <span class="hljs-number">0</span>);<br>&#125;<br></code></pre></td></tr></table></figure>

<ul>
<li>currentUser新增planetCode</li>
</ul>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><code class="hljs typescript"><span class="hljs-keyword">type</span> <span class="hljs-title class_">CurrentUser</span> = &#123;<br>  id?: <span class="hljs-built_in">number</span>;<br>  userAccount?: <span class="hljs-built_in">string</span>;<br>  username?: <span class="hljs-built_in">string</span>;<br>  avatarUrl?: <span class="hljs-built_in">string</span>;<br>  gender?: <span class="hljs-built_in">string</span>;<br>  phone?: <span class="hljs-built_in">string</span>;<br>  email?: <span class="hljs-built_in">string</span>;<br>  createTime?: <span class="hljs-title class_">Date</span>;<br>  userStatus?: <span class="hljs-built_in">string</span>;<br>  userRole?: <span class="hljs-built_in">number</span>;<br>  planetCode?: <span class="hljs-built_in">string</span>;<br>&#125;;<br></code></pre></td></tr></table></figure>

<ul>
<li>管理页用户信息新增planetCode</li>
</ul>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs typescript">&#123;<br>  <span class="hljs-attr">title</span>: <span class="hljs-string">&#x27;星球编号&#x27;</span>,<br>  <span class="hljs-attr">dataIndex</span>: <span class="hljs-string">&#x27;planetCode&#x27;</span>,<br>  <span class="hljs-attr">copyable</span>: <span class="hljs-literal">true</span>,<br>&#125;,<br></code></pre></td></tr></table></figure>

<h4 id="新增星球编号注册必填项完成-管理页用户信息正常显示"><a href="#新增星球编号注册必填项完成-管理页用户信息正常显示" class="headerlink" title="新增星球编号注册必填项完成, 管理页用户信息正常显示"></a>新增星球编号注册必填项完成, 管理页用户信息正常显示</h4><h2 id="返回通用对象"><a href="#返回通用对象" class="headerlink" title="返回通用对象"></a>返回通用对象</h2><ul>
<li><h5 id="自定义通用返回对象-BaseResponse"><a href="#自定义通用返回对象-BaseResponse" class="headerlink" title="自定义通用返回对象 - BaseResponse"></a>自定义通用返回对象 - BaseResponse</h5></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">   * 状态码</span><br><span class="hljs-comment">   */</span><br>  <span class="hljs-keyword">private</span> <span class="hljs-type">int</span> code;<br>  <br>  <span class="hljs-comment">/**</span><br><span class="hljs-comment">   * 数据</span><br><span class="hljs-comment">   */</span><br>  <span class="hljs-keyword">private</span> T data;<br>  <br>  <span class="hljs-comment">/**</span><br><span class="hljs-comment">   * 信息</span><br><span class="hljs-comment">   */</span><br>  <span class="hljs-keyword">private</span> String message;<br>  <br>  <span class="hljs-comment">/**</span><br><span class="hljs-comment">   * 描述</span><br><span class="hljs-comment">   */</span><br>  <span class="hljs-keyword">private</span> String description;<br></code></pre></td></tr></table></figure>

<ul>
<li><h5 id="通用返回对象多种构造器"><a href="#通用返回对象多种构造器" class="headerlink" title="通用返回对象多种构造器"></a>通用返回对象多种构造器</h5></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">    * 封装执行成功的结果</span><br><span class="hljs-comment">    *</span><br><span class="hljs-comment">    * <span class="hljs-doctag">@param</span> code        状态码</span><br><span class="hljs-comment">    * <span class="hljs-doctag">@param</span> data        数据</span><br><span class="hljs-comment">    * <span class="hljs-doctag">@param</span> message     信息</span><br><span class="hljs-comment">    * <span class="hljs-doctag">@param</span> description 描述</span><br><span class="hljs-comment">    */</span><br>   <span class="hljs-keyword">public</span> <span class="hljs-title function_">BaseResponse</span><span class="hljs-params">(<span class="hljs-type">int</span> code, T data, String message, String description)</span> &#123;<br>       <span class="hljs-built_in">this</span>.code = code;<br>       <span class="hljs-built_in">this</span>.data = data;<br>       <span class="hljs-built_in">this</span>.message = message;<br>       <span class="hljs-built_in">this</span>.description = description;<br>   &#125;<br></code></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">    * 封装全局自定义异常</span><br><span class="hljs-comment">    *</span><br><span class="hljs-comment">    * <span class="hljs-doctag">@param</span> code        状态码</span><br><span class="hljs-comment">    * <span class="hljs-doctag">@param</span> message     信息</span><br><span class="hljs-comment">    * <span class="hljs-doctag">@param</span> description 描述</span><br><span class="hljs-comment">    */</span><br>   <span class="hljs-keyword">public</span> <span class="hljs-title function_">BaseResponse</span><span class="hljs-params">(<span class="hljs-type">int</span> code, String message, String description)</span> &#123;<br>       <span class="hljs-built_in">this</span>.code = code;<br>       <span class="hljs-built_in">this</span>.data = <span class="hljs-literal">null</span>;<br>       <span class="hljs-built_in">this</span>.message = message;<br>       <span class="hljs-built_in">this</span>.description = description;<br>   &#125;<br></code></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">   * 封装局部自定义异常</span><br><span class="hljs-comment">   *</span><br><span class="hljs-comment">   * <span class="hljs-doctag">@param</span> errorCode 自定义异常态</span><br><span class="hljs-comment">   */</span><br>  <span class="hljs-keyword">public</span> <span class="hljs-title function_">BaseResponse</span><span class="hljs-params">(ErrorCode errorCode)</span> &#123;<br>      <span class="hljs-built_in">this</span>.code = errorCode.getCode();<br>      <span class="hljs-built_in">this</span>.data = <span class="hljs-literal">null</span>;<br>      <span class="hljs-built_in">this</span>.message = errorCode.getMessage();<br>      <span class="hljs-built_in">this</span>.description = errorCode.getDescription();<br>  &#125;<br></code></pre></td></tr></table></figure>

<ul>
<li><h5 id="通用返回对象添加getter-setter方法"><a href="#通用返回对象添加getter-setter方法" class="headerlink" title="通用返回对象添加getter, setter方法"></a>通用返回对象添加getter, setter方法</h5></li>
<li><h5 id="这一步一定不要忘记啊-我就因为没加这俩方法-测试登录注册就报406错误"><a href="#这一步一定不要忘记啊-我就因为没加这俩方法-测试登录注册就报406错误" class="headerlink" title="这一步一定不要忘记啊, 我就因为没加这俩方法, 测试登录注册就报406错误"></a>这一步一定不要忘记啊, 我就因为没加这俩方法, 测试登录注册就报406错误</h5></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-type">int</span> <span class="hljs-title function_">getCode</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-keyword">return</span> code;<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> T <span class="hljs-title function_">getData</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-keyword">return</span> data;<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> String <span class="hljs-title function_">getMessage</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-keyword">return</span> message;<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> String <span class="hljs-title function_">getDescription</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-keyword">return</span> description;<br>    &#125;<br></code></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">setCode</span><span class="hljs-params">(<span class="hljs-type">int</span> code)</span> &#123;<br>    <span class="hljs-built_in">this</span>.code = code;<br>&#125;<br><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">setData</span><span class="hljs-params">(T data)</span> &#123;<br>    <span class="hljs-built_in">this</span>.data = data;<br>&#125;<br><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">setMessage</span><span class="hljs-params">(String message)</span> &#123;<br>    <span class="hljs-built_in">this</span>.message = message;<br>&#125;<br><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">setDescription</span><span class="hljs-params">(String description)</span> &#123;<br>    <span class="hljs-built_in">this</span>.description = description;<br>&#125;<br></code></pre></td></tr></table></figure>

<h4 id="返回成功的话-就是返回成功状态-即返回状态码-数据-“ok”-message-“”-description"><a href="#返回成功的话-就是返回成功状态-即返回状态码-数据-“ok”-message-“”-description" class="headerlink" title="返回成功的话, 就是返回成功状态, 即返回状态码 + 数据 + “ok”(message) + “”(description)"></a>返回成功的话, 就是返回成功状态, 即返回状态码 + 数据 + “ok”(message) + “”(description)</h4><ul>
<li><h5 id="封装返回成功状态下通用对象的方法"><a href="#封装返回成功状态下通用对象的方法" class="headerlink" title="封装返回成功状态下通用对象的方法"></a>封装返回成功状态下通用对象的方法</h5></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">    * 封装调用执行成功</span><br><span class="hljs-comment">    *</span><br><span class="hljs-comment">    * <span class="hljs-doctag">@param</span> data 返回数据</span><br><span class="hljs-comment">    * <span class="hljs-doctag">@param</span> &lt;T&gt;  数据类型</span><br><span class="hljs-comment">    * <span class="hljs-doctag">@return</span> 执行成功信息</span><br><span class="hljs-comment">    */</span><br>   <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> &lt;T&gt; BaseResponse&lt;T&gt; <span class="hljs-title function_">success</span><span class="hljs-params">(T data)</span> &#123;<br>       <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">BaseResponse</span>&lt;&gt;(<span class="hljs-number">0</span>, data, <span class="hljs-string">&quot;ok&quot;</span>, <span class="hljs-string">&quot;&quot;</span>);<br>   &#125;<br></code></pre></td></tr></table></figure>

<h4 id="返回失败的话-就是返回失败状态-即返回状态码-null-data-message-description"><a href="#返回失败的话-就是返回失败状态-即返回状态码-null-data-message-description" class="headerlink" title="返回失败的话, 就是返回失败状态, 即返回状态码 + null(data) + message + description"></a>返回失败的话, 就是返回失败状态, 即返回状态码 + null(data) + message + description</h4><h4 id="实现思路-这里我们打算封装自定义异常类和全局异常处理器-当业务中出现错误-不会返回失败结果-而是抛出相应的异常-并由全局异常处理器捕获-再由全局异常处理器来返回对应失败状态"><a href="#实现思路-这里我们打算封装自定义异常类和全局异常处理器-当业务中出现错误-不会返回失败结果-而是抛出相应的异常-并由全局异常处理器捕获-再由全局异常处理器来返回对应失败状态" class="headerlink" title="实现思路: 这里我们打算封装自定义异常类和全局异常处理器, 当业务中出现错误, 不会返回失败结果, 而是抛出相应的异常, 并由全局异常处理器捕获, 再由全局异常处理器来返回对应失败状态"></a>实现思路: 这里我们打算封装自定义异常类和全局异常处理器, 当业务中出现错误, 不会返回失败结果, 而是抛出相应的异常, 并由全局异常处理器捕获, 再由全局异常处理器来返回对应失败状态</h4><h2 id="封装自定义异常BusinessException"><a href="#封装自定义异常BusinessException" class="headerlink" title="封装自定义异常BusinessException"></a>封装自定义异常BusinessException</h2><h5 id="自定义异常成员属性"><a href="#自定义异常成员属性" class="headerlink" title="自定义异常成员属性"></a>自定义异常成员属性</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">BusinessException</span> <span class="hljs-keyword">extends</span> <span class="hljs-title class_">RuntimeException</span> &#123;<br> <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 状态码</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">private</span> <span class="hljs-keyword">final</span> <span class="hljs-type">int</span> code;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 异常描述</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">private</span> <span class="hljs-keyword">final</span> String description;<br>................<br>&#125;<br></code></pre></td></tr></table></figure>

<ul>
<li><h5 id="这里我们会发现-自定义异常里没有定义message属性-这是因为自定义异常继承了RuntimeException-在执行构造方法时-执行super-message-即可设置自定义的异常信息-再通过e-getMessage-方法来获取异常信息"><a href="#这里我们会发现-自定义异常里没有定义message属性-这是因为自定义异常继承了RuntimeException-在执行构造方法时-执行super-message-即可设置自定义的异常信息-再通过e-getMessage-方法来获取异常信息" class="headerlink" title="这里我们会发现, 自定义异常里没有定义message属性. 这是因为自定义异常继承了RuntimeException, 在执行构造方法时, 执行super(message)即可设置自定义的异常信息, 再通过e.getMessage()方法来获取异常信息"></a>这里我们会发现, 自定义异常里没有定义message属性. 这是因为自定义异常继承了RuntimeException, 在执行构造方法时, 执行super(message)即可设置自定义的异常信息, 再通过e.getMessage()方法来获取异常信息</h5></li>
<li><h5 id="自定义异常多种构造器"><a href="#自定义异常多种构造器" class="headerlink" title="自定义异常多种构造器"></a>自定义异常多种构造器</h5></li>
<li><h5 id="局部业务代码下自定义的异常信息"><a href="#局部业务代码下自定义的异常信息" class="headerlink" title="局部业务代码下自定义的异常信息"></a>局部业务代码下自定义的异常信息</h5></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">   * 封装局部自定义异常</span><br><span class="hljs-comment">   *</span><br><span class="hljs-comment">   * <span class="hljs-doctag">@param</span> message     异常信息</span><br><span class="hljs-comment">   * <span class="hljs-doctag">@param</span> code        异常状态码</span><br><span class="hljs-comment">   * <span class="hljs-doctag">@param</span> description 异常描述</span><br><span class="hljs-comment">   */</span><br>  <span class="hljs-keyword">public</span> <span class="hljs-title function_">BusinessException</span><span class="hljs-params">(String message, <span class="hljs-type">int</span> code, String description)</span> &#123;<br>      <span class="hljs-built_in">super</span>(message);<br>      <span class="hljs-built_in">this</span>.code = code;<br>      <span class="hljs-built_in">this</span>.description = description;<br>  &#125;<br></code></pre></td></tr></table></figure>

<ul>
<li><h5 id="封装全局自定义异常-无描述"><a href="#封装全局自定义异常-无描述" class="headerlink" title="封装全局自定义异常(无描述)"></a>封装全局自定义异常(无描述)</h5></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">   * 封装全局自定义异常(无描述)</span><br><span class="hljs-comment">   *</span><br><span class="hljs-comment">   * <span class="hljs-doctag">@param</span> errorCode 自定义异常态</span><br><span class="hljs-comment">   */</span><br>  <span class="hljs-keyword">public</span> <span class="hljs-title function_">BusinessException</span><span class="hljs-params">(ErrorCode errorCode)</span> &#123;<br>      <span class="hljs-built_in">super</span>(errorCode.getMessage());<br>      <span class="hljs-built_in">this</span>.code = errorCode.getCode();<br>      <span class="hljs-built_in">this</span>.description = errorCode.getDescription();<br>  &#125;<br></code></pre></td></tr></table></figure>

<ul>
<li><h5 id="封装全局自定义异常-有描述"><a href="#封装全局自定义异常-有描述" class="headerlink" title="封装全局自定义异常(有描述)"></a>封装全局自定义异常(有描述)</h5></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">  * 封装全局自定义异常(有描述)</span><br><span class="hljs-comment">  *</span><br><span class="hljs-comment">  * <span class="hljs-doctag">@param</span> errorCode   自定义异常态</span><br><span class="hljs-comment">  * <span class="hljs-doctag">@param</span> description 异常描述</span><br><span class="hljs-comment">  */</span><br> <span class="hljs-keyword">public</span> <span class="hljs-title function_">BusinessException</span><span class="hljs-params">(ErrorCode errorCode, String description)</span> &#123;<br>     <span class="hljs-built_in">super</span>(errorCode.getMessage());<br>     <span class="hljs-built_in">this</span>.code = errorCode.getCode();<br>     <span class="hljs-built_in">this</span>.description = description;<br> &#125;<br></code></pre></td></tr></table></figure>

<h3 id="补充-封装全局自定义异常信息-ErrorCode-枚举类"><a href="#补充-封装全局自定义异常信息-ErrorCode-枚举类" class="headerlink" title="补充: 封装全局自定义异常信息 ErrorCode (枚举类)"></a>补充: 封装全局自定义异常信息 ErrorCode (枚举类)</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">enum</span> <span class="hljs-title class_">ErrorCode</span> &#123;<br>	..............<br>&#125;<br></code></pre></td></tr></table></figure>

<ul>
<li><h5 id="枚举值-需持续完善更新"><a href="#枚举值-需持续完善更新" class="headerlink" title="枚举值 (需持续完善更新)"></a>枚举值 (需持续完善更新)</h5></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs java">PARMS_ERROR(<span class="hljs-number">40000</span>, <span class="hljs-string">&quot;请求参数错误&quot;</span>, <span class="hljs-string">&quot;&quot;</span>),<br>NULL_ERROR(<span class="hljs-number">40001</span>, <span class="hljs-string">&quot;请求数据为空&quot;</span>, <span class="hljs-string">&quot;&quot;</span>),<br>UPDATE_ERROR(<span class="hljs-number">40002</span>, <span class="hljs-string">&quot;操作数据库失败&quot;</span>, <span class="hljs-string">&quot;&quot;</span>),<br>NOT_LOGIN(<span class="hljs-number">40003</span>, <span class="hljs-string">&quot;未登录&quot;</span>, <span class="hljs-string">&quot;&quot;</span>),<br>NOT_REGISTER(<span class="hljs-number">40004</span>, <span class="hljs-string">&quot;未注册&quot;</span>, <span class="hljs-string">&quot;&quot;</span>),<br>NO_AUTH(<span class="hljs-number">40005</span>, <span class="hljs-string">&quot;无权限&quot;</span>, <span class="hljs-string">&quot;&quot;</span>),<br>SYSTEM_ERROR(<span class="hljs-number">10000</span>, <span class="hljs-string">&quot;系统内部异常&quot;</span>, <span class="hljs-string">&quot;&quot;</span>);<br></code></pre></td></tr></table></figure>

<ul>
<li><h5 id="成员属性"><a href="#成员属性" class="headerlink" title="成员属性"></a>成员属性</h5></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">    * 状态码</span><br><span class="hljs-comment">    */</span><br>   <span class="hljs-keyword">private</span> <span class="hljs-keyword">final</span> <span class="hljs-type">int</span> code;<br><br>   <span class="hljs-comment">/**</span><br><span class="hljs-comment">    * 状态码信息</span><br><span class="hljs-comment">    */</span><br>   <span class="hljs-keyword">private</span> <span class="hljs-keyword">final</span> String message;<br><br>   <span class="hljs-comment">/**</span><br><span class="hljs-comment">    * 状态码描述</span><br><span class="hljs-comment">    */</span><br>   <span class="hljs-keyword">private</span> <span class="hljs-keyword">final</span> String description;<br></code></pre></td></tr></table></figure>

<ul>
<li><h5 id="构造器和-getter-方法"><a href="#构造器和-getter-方法" class="headerlink" title="构造器和 getter 方法"></a>构造器和 getter 方法</h5></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">   * <span class="hljs-doctag">@param</span> code        状态码</span><br><span class="hljs-comment">   * <span class="hljs-doctag">@param</span> message     信息</span><br><span class="hljs-comment">   * <span class="hljs-doctag">@param</span> description 描述</span><br><span class="hljs-comment">   */</span><br>  ErrorCode(<span class="hljs-type">int</span> code, String message, String description) &#123;<br>      <span class="hljs-built_in">this</span>.code = code;<br>      <span class="hljs-built_in">this</span>.message = message;<br>      <span class="hljs-built_in">this</span>.description = description;<br>  &#125;<br><br>  <span class="hljs-keyword">public</span> <span class="hljs-type">int</span> <span class="hljs-title function_">getCode</span><span class="hljs-params">()</span> &#123;<br>      <span class="hljs-keyword">return</span> code;<br>  &#125;<br><br>  <span class="hljs-keyword">public</span> String <span class="hljs-title function_">getMessage</span><span class="hljs-params">()</span> &#123;<br>      <span class="hljs-keyword">return</span> message;<br>  &#125;<br><br>  <span class="hljs-keyword">public</span> String <span class="hljs-title function_">getDescription</span><span class="hljs-params">()</span> &#123;<br>      <span class="hljs-keyword">return</span> description;<br>  &#125;<br></code></pre></td></tr></table></figure>

<h5 id="自定义异常-getter-方法"><a href="#自定义异常-getter-方法" class="headerlink" title="自定义异常 getter 方法"></a>自定义异常 getter 方法</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-type">int</span> <span class="hljs-title function_">getCode</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-keyword">return</span> code;<br>    &#125;<br>    <br><span class="hljs-keyword">public</span> String <span class="hljs-title function_">getDescription</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-keyword">return</span> description;<br>    &#125;    <br></code></pre></td></tr></table></figure>

<h2 id="封装全局异常处理器"><a href="#封装全局异常处理器" class="headerlink" title="封装全局异常处理器"></a>封装全局异常处理器</h2><h5 id=""><a href="#" class="headerlink" title=""></a><!--熟悉全局异常处理器的写法和实现过程, 了解其实现原理--></h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@RestControllerAdvice</span><br><span class="hljs-meta">@Slf4j</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">GlobalExceptionHandler</span> &#123;<br>	....................<br>&#125;<br></code></pre></td></tr></table></figure>

<ul>
<li><h5 id="捕获自定义异常对象"><a href="#捕获自定义异常对象" class="headerlink" title="捕获自定义异常对象"></a>捕获自定义异常对象</h5></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">    * 捕获自定义异常对象</span><br><span class="hljs-comment">    *</span><br><span class="hljs-comment">    * <span class="hljs-doctag">@param</span> e 自定义异常</span><br><span class="hljs-comment">    * <span class="hljs-doctag">@return</span> 封装返回执行失败异常信息</span><br><span class="hljs-comment">    */</span><br>   <span class="hljs-meta">@ExceptionHandler(BusinessException.class)</span><br>   <span class="hljs-keyword">public</span> BaseResponse <span class="hljs-title function_">locallyCustomException</span><span class="hljs-params">(BusinessException e)</span> &#123;<br>       log.error(<span class="hljs-string">&quot;businessException: &quot;</span> + e.getMessage(), e);<br>       log.info(<span class="hljs-string">&quot;&quot;</span> + e.getCode());<br>       log.info(e.getMessage());<br>       log.info(e.getDescription());<br>       <span class="hljs-keyword">return</span> ResultUtils.error(e.getCode(), e.getMessage(), e.getDescription());<br>   &#125;<br></code></pre></td></tr></table></figure>

<ul>
<li><h5 id="捕获其他异常"><a href="#捕获其他异常" class="headerlink" title="捕获其他异常"></a>捕获其他异常</h5></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">    * 捕获其他异常</span><br><span class="hljs-comment">    *</span><br><span class="hljs-comment">    * <span class="hljs-doctag">@param</span> e 其他异常</span><br><span class="hljs-comment">    * <span class="hljs-doctag">@return</span> 封装返回请求失败异常信息</span><br><span class="hljs-comment">    */</span><br>   <span class="hljs-meta">@ExceptionHandler(RuntimeException.class)</span><br>   <span class="hljs-keyword">public</span> BaseResponse <span class="hljs-title function_">globalCustomException</span><span class="hljs-params">(Exception e)</span> &#123;<br>       log.error(<span class="hljs-string">&quot;runtimeException&quot;</span>, e);<br>       <span class="hljs-keyword">return</span> ResultUtils.error(ErrorCode.SYSTEM_ERROR);<br>   &#125;<br></code></pre></td></tr></table></figure>

<h2 id="修改后端返回给前端的数据-全部更新为直接返回通用对象-成功状态-或抛出业务异常再由全局异常处理器返回通用对象-失败状态"><a href="#修改后端返回给前端的数据-全部更新为直接返回通用对象-成功状态-或抛出业务异常再由全局异常处理器返回通用对象-失败状态" class="headerlink" title="修改后端返回给前端的数据, 全部更新为直接返回通用对象(成功状态)或抛出业务异常再由全局异常处理器返回通用对象(失败状态)"></a>修改后端返回给前端的数据, 全部更新为直接返回通用对象(成功状态)或抛出业务异常再由全局异常处理器返回通用对象(失败状态)</h2><ul>
<li><h5 id="成功状态-controller层"><a href="#成功状态-controller层" class="headerlink" title="成功状态 (controller层)"></a>成功状态 (controller层)</h5></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 用户注册</span><br><span class="hljs-comment">     *</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> userRegisterRequest 注册信息封装类</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span> id</span><br><span class="hljs-comment">     */</span><br><span class="hljs-keyword">public</span> BaseResponse&lt;Long&gt; <span class="hljs-title function_">userRegister</span><span class="hljs-params">(<span class="hljs-meta">@RequestBody</span> UserRegisterRequest userRegisterRequest)</span> &#123;<br>    ......................<br>    <span class="hljs-keyword">return</span> ResultUtils.success(userRegister);<br>&#125;<br></code></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 用户登录</span><br><span class="hljs-comment">     *</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> userLoginRequest 登录信息封装</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> request          request</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span> User</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-meta">@PostMapping(&quot;/login&quot;)</span><br>    <span class="hljs-keyword">public</span> BaseResponse&lt;User&gt; <span class="hljs-title function_">userLogin</span><span class="hljs-params">(<span class="hljs-meta">@RequestBody</span> UserLoginRequest userLoginRequest, HttpServletRequest request)</span> &#123;<br>        .........................<br>        <span class="hljs-keyword">return</span> ResultUtils.success(userLogin);<br>    &#125;<br></code></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 用户登录</span><br><span class="hljs-comment">     *</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> request request</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span> int</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-meta">@PostMapping(&quot;/logout&quot;)</span><br>    <span class="hljs-keyword">public</span> BaseResponse&lt;String&gt; <span class="hljs-title function_">userLogout</span><span class="hljs-params">(HttpServletRequest request)</span> &#123;<br>        ..........................<br>        <span class="hljs-keyword">return</span> ResultUtils.success(userLogout);<br>    &#125;<br></code></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 获取当前用户登录态</span><br><span class="hljs-comment">     *</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> request request</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span> 当前用户信息</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-meta">@GetMapping(&quot;/currentUser&quot;)</span><br>    <span class="hljs-keyword">public</span> BaseResponse&lt;User&gt; <span class="hljs-title function_">getCurrentUser</span><span class="hljs-params">(HttpServletRequest request)</span> &#123;<br>        ............................<br>        <span class="hljs-keyword">return</span> ResultUtils.success(user);<br>    &#125;<br></code></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 查询用户</span><br><span class="hljs-comment">     *</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> username 用户名</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span> 查到的用户</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-meta">@GetMapping(&quot;/search&quot;)</span><br>    <span class="hljs-keyword">public</span> BaseResponse&lt;List&lt;User&gt;&gt; <span class="hljs-title function_">userSearch</span><span class="hljs-params">(String username, HttpServletRequest request)</span> &#123;<br>        .............................<br>        <span class="hljs-keyword">return</span> ResultUtils.success(users);<br>    &#125;<br></code></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 删除用户</span><br><span class="hljs-comment">     *</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> id 用户id</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span> true/false</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-meta">@DeleteMapping(&quot;/delete&quot;)</span><br>    <span class="hljs-keyword">public</span> BaseResponse&lt;Boolean&gt; <span class="hljs-title function_">userDelete</span><span class="hljs-params">(Long id, HttpServletRequest request)</span> &#123;<br>        ..............................<br>        <span class="hljs-keyword">return</span> ResultUtils.success(removeById);<br>    &#125;<br></code></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">if</span> (StringUtils.isAnyBlank(userAccount, userPassword, checkPassword, planetCode))<br>            <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">BusinessException</span>(PARMS_ERROR);<br></code></pre></td></tr></table></figure>

<h5 id="失败状态"><a href="#失败状态" class="headerlink" title="失败状态"></a>失败状态</h5><ul>
<li><h5 id="controller层-register-login-logout-currentUser-search-delete-下的controller参数校验"><a href="#controller层-register-login-logout-currentUser-search-delete-下的controller参数校验" class="headerlink" title="controller层    register    login    logout    currentUser    search    delete 下的controller参数校验"></a>controller层    register    login    logout    currentUser    search    delete 下的controller参数校验</h5></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">if</span> (StringUtils.isAnyBlank(userAccount, userPassword, checkPassword, planetCode))<br>       <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">BusinessException</span>(PARMS_ERROR);<br></code></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">if</span> (StringUtils.isAnyBlank(userAccount, userPassword))<br>        <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">BusinessException</span>(PARMS_ERROR);<br></code></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">if</span> (request == <span class="hljs-literal">null</span>)<br>        <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">BusinessException</span>(PARMS_ERROR);<br></code></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">if</span> (currentUser == <span class="hljs-literal">null</span>)<br>        <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">BusinessException</span>(PARMS_ERROR);<br></code></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">if</span> (!isAdmin(request))<br>         <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">BusinessException</span>(NO_AUTH);<br></code></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">if</span> (!isAdmin(request))<br>          <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">BusinessException</span>(NO_AUTH);<br><span class="hljs-keyword">if</span> (id &lt;= <span class="hljs-number">0</span>)<br>          <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">BusinessException</span>(PARMS_ERROR);<br></code></pre></td></tr></table></figure>

<ul>
<li><h5 id="service层-register-下的参数校验"><a href="#service层-register-下的参数校验" class="headerlink" title="service层    register 下的参数校验"></a>service层    register 下的参数校验</h5></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">// 1.1.账户, 密码, 二次密码不能为空</span><br>        <span class="hljs-keyword">if</span> (StringUtils.isAnyBlank(userAccount, userPassword, checkPassword))<br>            <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">BusinessException</span>(PARMS_ERROR);<br></code></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">// 1.2.账户不小于4位</span><br>        <span class="hljs-keyword">if</span> (userAccount.length() &lt; <span class="hljs-number">4</span>)<br>            <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">BusinessException</span>(<span class="hljs-string">&quot;账户不符合要求&quot;</span>, <span class="hljs-number">50000</span>, <span class="hljs-string">&quot;账户小于4位&quot;</span>);<br></code></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">// 1.3.账户不包含特殊字符</span><br>        <span class="hljs-type">String</span> <span class="hljs-variable">pattern</span> <span class="hljs-operator">=</span> <span class="hljs-string">&quot;.*[\\s`~!@#$%^&amp;*()+=|&#123;&#125;&#x27;:;&#x27;,\\[\\].&lt;&gt;/?~！@#￥%……&amp;*（）——+|&#123;&#125;【】‘；：”“’。，、？\\\\]+.*&quot;</span>;<br>        <span class="hljs-keyword">if</span> (Pattern.matches(pattern, userAccount))<br>            <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">BusinessException</span>(<span class="hljs-string">&quot;账户不符合要求&quot;</span>, <span class="hljs-number">50001</span>, <span class="hljs-string">&quot;账户包含特殊字符&quot;</span>);<br></code></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">// 1.4.用户密码不小于8位</span><br>        <span class="hljs-keyword">if</span> (userPassword.length() &lt; <span class="hljs-number">8</span>)<br>            <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">BusinessException</span>(<span class="hljs-string">&quot;密码不符合要求&quot;</span>, <span class="hljs-number">60000</span>, <span class="hljs-string">&quot;用户密码小于8位&quot;</span>);<br></code></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">// 1.5.二次密码与密码相同</span><br>        <span class="hljs-keyword">if</span> (!userPassword.equals(checkPassword))<br>            <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">BusinessException</span>(<span class="hljs-string">&quot;二次密码不符合要求&quot;</span>, <span class="hljs-number">60001</span>, <span class="hljs-string">&quot;二次密码与密码不相同&quot;</span>);<br></code></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">// 1.6.星球编号不能超过5位</span><br>        <span class="hljs-keyword">if</span> (planetCode.length() &gt; <span class="hljs-number">5</span>)<br>            <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">BusinessException</span>(<span class="hljs-string">&quot;星球编号不符合要求&quot;</span>, <span class="hljs-number">60002</span>, <span class="hljs-string">&quot;星球编号超过5位&quot;</span>);<br></code></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">// 1.7.账户不能重复</span><br>        QueryWrapper&lt;User&gt; ua_lqw = <span class="hljs-keyword">new</span> <span class="hljs-title class_">QueryWrapper</span>&lt;&gt;(); <span class="hljs-comment">// LambdaQueryWrapper&lt;User&gt; userLambdaQueryWrapper = new LambdaQueryWrapper&lt;&gt;();</span><br>        ua_lqw.eq(<span class="hljs-string">&quot;user_account&quot;</span>, userAccount); <span class="hljs-comment">// userLambdaQueryWrapper.eq(User::getUserAccount, userAccount);</span><br>        <span class="hljs-type">Long</span> <span class="hljs-variable">ua_count</span> <span class="hljs-operator">=</span> userMapper.selectCount(ua_lqw);   <span class="hljs-comment">// long count = this.count(lqw);</span><br>        <span class="hljs-keyword">if</span> (ua_count &gt; <span class="hljs-number">0</span>)<br>            <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">BusinessException</span>(<span class="hljs-string">&quot;账户不符合要求&quot;</span>, <span class="hljs-number">50002</span>, <span class="hljs-string">&quot;账户重复&quot;</span>);<br></code></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">// 1.8.星球编号不能重复</span><br>        QueryWrapper&lt;User&gt; pc_lqw = <span class="hljs-keyword">new</span> <span class="hljs-title class_">QueryWrapper</span>&lt;&gt;();<br>        pc_lqw.eq(<span class="hljs-string">&quot;planet_code&quot;</span>, planetCode);<br>        <span class="hljs-type">Long</span> <span class="hljs-variable">pc_count</span> <span class="hljs-operator">=</span> userMapper.selectCount(pc_lqw);<br>        <span class="hljs-keyword">if</span> (pc_count &gt; <span class="hljs-number">0</span>)<br>            <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">BusinessException</span>(<span class="hljs-string">&quot;星球编号不符合要求&quot;</span>, <span class="hljs-number">60003</span>, <span class="hljs-string">&quot;星球编号重复&quot;</span>);<br></code></pre></td></tr></table></figure>

<ul>
<li><h5 id="service层-login下的参数校验"><a href="#service层-login下的参数校验" class="headerlink" title="service层    login下的参数校验"></a>service层    login下的参数校验</h5></li>
</ul>
<figure class="highlight reasonml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs reasonml"><span class="hljs-comment">// 1.1.账户, 密码不能为空</span><br>        <span class="hljs-keyword">if</span> (<span class="hljs-module-access"><span class="hljs-module"><span class="hljs-identifier">StringUtils</span>.</span></span>is<span class="hljs-constructor">AnyBlank(<span class="hljs-params">userAccount</span>, <span class="hljs-params">userPassword</span>)</span>)<br>            throw <span class="hljs-keyword">new</span> <span class="hljs-constructor">BusinessException(PARMS_ERROR)</span>;<br></code></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">// 1.2.账户不小于4位</span><br>        <span class="hljs-keyword">if</span> (userAccount.length() &lt; <span class="hljs-number">4</span>)<br>            <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">BusinessException</span>(<span class="hljs-string">&quot;账户不符合要求&quot;</span>, <span class="hljs-number">50000</span>, <span class="hljs-string">&quot;账户小于4位&quot;</span>);<br></code></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">// 1.3.用户密码不小于8位</span><br>        <span class="hljs-keyword">if</span> (userPassword.length() &lt; <span class="hljs-number">8</span>)<br>            <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">BusinessException</span>(<span class="hljs-string">&quot;密码不符合要求&quot;</span>, <span class="hljs-number">60000</span>, <span class="hljs-string">&quot;用户密码小于8位&quot;</span>);<br></code></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">// 1.4.账户不包含特殊字符</span><br>        <span class="hljs-type">String</span> <span class="hljs-variable">pattern</span> <span class="hljs-operator">=</span> <span class="hljs-string">&quot;.*[\\s`~!@#$%^&amp;*()+=|&#123;&#125;&#x27;:;&#x27;,\\[\\].&lt;&gt;/?~！@#￥%……&amp;*（）——+|&#123;&#125;【】‘；：”“’。，、？\\\\]+.*&quot;</span>;<br>        <span class="hljs-keyword">if</span> (Pattern.matches(pattern, userAccount))<br>            <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">BusinessException</span>(<span class="hljs-string">&quot;账户不符合要求&quot;</span>, <span class="hljs-number">50001</span>, <span class="hljs-string">&quot;账户包含特殊字符&quot;</span>);<br></code></pre></td></tr></table></figure>

<ul>
<li><h5 id="service层-logout下的返回信息"><a href="#service层-logout下的返回信息" class="headerlink" title="service层    logout下的返回信息"></a>service层    logout下的返回信息</h5></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs java">request.getSession().removeAttribute(USER_LOGIN_STATE);<br>        <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;注销成功&quot;</span>;<br></code></pre></td></tr></table></figure>

<h5 id="自定义异常并由全局异常处理器成功处理-功能基本完成"><a href="#自定义异常并由全局异常处理器成功处理-功能基本完成" class="headerlink" title="自定义异常并由全局异常处理器成功处理, 功能基本完成"></a>自定义异常并由全局异常处理器成功处理, 功能基本完成</h5><h2 id="前端适配后端的通用返回对象"><a href="#前端适配后端的通用返回对象" class="headerlink" title="前端适配后端的通用返回对象"></a>前端适配后端的通用返回对象</h2><ul>
<li><h5 id="前端定义通用返回对象-services-ant-design-pro-typings-d-ts"><a href="#前端定义通用返回对象-services-ant-design-pro-typings-d-ts" class="headerlink" title="前端定义通用返回对象(services/ant-design-pro/typings.d.ts)"></a>前端定义通用返回对象(services/ant-design-pro/typings.d.ts)</h5></li>
</ul>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs typescript"><span class="hljs-comment">/**</span><br><span class="hljs-comment">   * 通用返回类</span><br><span class="hljs-comment">   */</span><br>  <span class="hljs-keyword">type</span> <span class="hljs-title class_">BaseResponse</span>&lt;T&gt; = &#123;<br>    <span class="hljs-attr">code</span>: <span class="hljs-built_in">number</span>,<br>    <span class="hljs-attr">data</span>: T,<br>    <span class="hljs-attr">message</span>: <span class="hljs-built_in">string</span>,<br>    <span class="hljs-attr">description</span>: <span class="hljs-built_in">string</span><br>  &#125;<br></code></pre></td></tr></table></figure>

<ul>
<li><h5 id="封装各接口响应类型-services-ant-design-pro-api-ts"><a href="#封装各接口响应类型-services-ant-design-pro-api-ts" class="headerlink" title="封装各接口响应类型(services/ant-design-pro/api.ts)"></a>封装各接口响应类型(services/ant-design-pro/api.ts)</h5></li>
</ul>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs typescript"><span class="hljs-keyword">return</span> request&lt;<span class="hljs-variable constant_">API</span>.<span class="hljs-property">BaseResponse</span>&lt;<span class="hljs-variable constant_">API</span>.<span class="hljs-property">CurrentUser</span>&gt;&gt;(<span class="hljs-string">&#x27;/api/user/currentUser&#x27;</span>, &#123;<br>  <span class="hljs-attr">method</span>: <span class="hljs-string">&#x27;GET&#x27;</span>,<br>  ...(options || &#123;&#125;),<br>&#125;);<br></code></pre></td></tr></table></figure>

<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs typescript"><span class="hljs-keyword">return</span> request&lt;<span class="hljs-variable constant_">API</span>.<span class="hljs-property">BaseResponse</span>&lt;<span class="hljs-variable constant_">API</span>.<span class="hljs-property">CurrentUser</span>&gt;&gt;(<span class="hljs-string">&#x27;/api/user/logout&#x27;</span>, &#123;<br>  <span class="hljs-attr">method</span>: <span class="hljs-string">&#x27;POST&#x27;</span>,<br>  ...(options || &#123;&#125;),<br>&#125;);<br></code></pre></td></tr></table></figure>

<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs typescript"><span class="hljs-keyword">return</span> request&lt;<span class="hljs-variable constant_">API</span>.<span class="hljs-property">BaseResponse</span>&lt;<span class="hljs-variable constant_">API</span>.<span class="hljs-property">CurrentUser</span>&gt;&gt;(<span class="hljs-string">&#x27;/api/user/login&#x27;</span>, &#123;<br>  <span class="hljs-attr">method</span>: <span class="hljs-string">&#x27;POST&#x27;</span>,<br>  <span class="hljs-attr">headers</span>: &#123;<br>    <span class="hljs-string">&#x27;Content-Type&#x27;</span>: <span class="hljs-string">&#x27;application/json&#x27;</span>,<br>  &#125;,<br>  <span class="hljs-attr">data</span>: body,<br>  ...(options || &#123;&#125;),<br>&#125;);<br></code></pre></td></tr></table></figure>

<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs typescript"><span class="hljs-keyword">return</span> request&lt;<span class="hljs-variable constant_">API</span>.<span class="hljs-property">BaseResponse</span>&lt;<span class="hljs-variable constant_">API</span>.<span class="hljs-property">CurrentUser</span>&gt;&gt;(<span class="hljs-string">&#x27;/api/user/register&#x27;</span>, &#123;<br>  <span class="hljs-attr">method</span>: <span class="hljs-string">&#x27;POST&#x27;</span>,<br>  <span class="hljs-attr">headers</span>: &#123;<br>    <span class="hljs-string">&#x27;Content-Type&#x27;</span>: <span class="hljs-string">&#x27;application/json&#x27;</span>,<br>  &#125;,<br>  <span class="hljs-attr">data</span>: body,<br>  ...(options || &#123;&#125;),<br>&#125;);<br></code></pre></td></tr></table></figure>

<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs typescript"><span class="hljs-keyword">return</span> request&lt;<span class="hljs-variable constant_">API</span>.<span class="hljs-property">BaseResponse</span>&lt;<span class="hljs-variable constant_">API</span>.<span class="hljs-property">CurrentUser</span>&gt;&gt;(<span class="hljs-string">&#x27;/api/user/search&#x27;</span>, &#123;<br>  <span class="hljs-attr">method</span>: <span class="hljs-string">&#x27;GET&#x27;</span>,<br>  ...(options || &#123;&#125;),<br>&#125;);<br></code></pre></td></tr></table></figure>

<ul>
<li><h5 id="修改登录-注册成功的校验逻辑"><a href="#修改登录-注册成功的校验逻辑" class="headerlink" title="修改登录/注册成功的校验逻辑"></a>修改登录/注册成功的校验逻辑</h5></li>
</ul>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs typescript"><span class="hljs-comment">// 登录</span><br><span class="hljs-keyword">const</span> data = <span class="hljs-keyword">await</span> <span class="hljs-title function_">login</span>(&#123;<br>  ...values,<br>  <span class="hljs-keyword">type</span>,<br>&#125;);<br><span class="hljs-comment">// 登录成功</span><br><span class="hljs-keyword">if</span> (data) &#123;<br>  <span class="hljs-keyword">const</span> defaultLoginSuccessMessage = <span class="hljs-string">&#x27;登录成功！&#x27;</span>;<br>  ................<br> &#125;<br></code></pre></td></tr></table></figure>

<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs typescript"><span class="hljs-comment">// 注册</span><br><span class="hljs-comment">// 返回id</span><br><span class="hljs-keyword">const</span> data = <span class="hljs-keyword">await</span> <span class="hljs-title function_">register</span>(&#123;<br>  ...values,<br>  <span class="hljs-keyword">type</span>,<br>&#125;);<br><span class="hljs-comment">// 注册成功</span><br><span class="hljs-keyword">if</span> (data) &#123;<br>  <span class="hljs-keyword">const</span> defaultLoginSuccessMessage = <span class="hljs-string">&#x27;注册成功！&#x27;</span>;<br>  ....................<br> &#125;<br></code></pre></td></tr></table></figure>

<ul>
<li><h5 id="这边还要记得修改两个地方"><a href="#这边还要记得修改两个地方" class="headerlink" title="这边还要记得修改两个地方:"></a>这边还要记得修改两个地方:</h5></li>
</ul>
<h6 id="修改管理员权限的校验-解决了无法正确访问到管理员页面的问题"><a href="#修改管理员权限的校验-解决了无法正确访问到管理员页面的问题" class="headerlink" title="修改管理员权限的校验 - 解决了无法正确访问到管理员页面的问题"></a>修改管理员权限的校验 - 解决了无法正确访问到管理员页面的问题</h6><figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs typescript"><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-keyword">function</span> <span class="hljs-title function_">access</span>(<span class="hljs-params">initialState: &#123; baseResponse?: API.BaseResponse&lt;API.CurrentUser&gt; &#125; | <span class="hljs-literal">undefined</span></span>) &#123;<br>  <span class="hljs-keyword">const</span> &#123; baseResponse &#125; = initialState ?? &#123;&#125;;<br>  <span class="hljs-comment">// 权限校验</span><br>  <span class="hljs-keyword">return</span> &#123;<br>    <span class="hljs-comment">// 校验管理员权限</span><br>    <span class="hljs-attr">canAdmin</span>: baseResponse &amp;&amp; baseResponse.<span class="hljs-property">data</span>.<span class="hljs-property">userRole</span> === <span class="hljs-number">1</span>,<br>  &#125;;<br>&#125;<br></code></pre></td></tr></table></figure>

<h6 id="修改查询在线用户信息-解决了在线用户信息页面无法正常显示的问题"><a href="#修改查询在线用户信息-解决了在线用户信息页面无法正常显示的问题" class="headerlink" title="修改查询在线用户信息 - 解决了在线用户信息页面无法正常显示的问题"></a>修改查询在线用户信息 - 解决了在线用户信息页面无法正常显示的问题</h6><figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs typescript"><span class="hljs-keyword">const</span> <span class="hljs-attr">columns</span>: <span class="hljs-title class_">ProColumns</span>&lt;<span class="hljs-variable constant_">API</span>.<span class="hljs-property">BaseResponse</span>&lt;<span class="hljs-variable constant_">API</span>.<span class="hljs-property">CurrentUser</span>&gt;&gt;[] = [<br>...............<br>]<br></code></pre></td></tr></table></figure>

<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs typescript">&lt;<span class="hljs-title class_">ProTable</span>&lt;<span class="hljs-variable constant_">API</span>.<span class="hljs-property">BaseResponse</span>&lt;<span class="hljs-variable constant_">API</span>.<span class="hljs-property">CurrentUser</span>&gt;&gt;<br>  columns=&#123;columns&#125;<br>  ............<br></code></pre></td></tr></table></figure>

<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><code class="hljs typescript"> <span class="hljs-comment">// 弹出异常信息</span><br>    message.<span class="hljs-title function_">error</span>(res.<span class="hljs-property">message</span>)<br>    <span class="hljs-comment">// 弹出异常描述</span><br>    message.<span class="hljs-title function_">error</span>(res.<span class="hljs-property">description</span>)<br>    <span class="hljs-comment">// 跳转至登录页</span><br>    history.<span class="hljs-title function_">replace</span>(&#123;<br>      <span class="hljs-attr">pathname</span>: <span class="hljs-string">&#x27;user/login&#x27;</span>,<br>      <span class="hljs-attr">search</span>: <span class="hljs-title function_">stringify</span>(&#123;<br>        <span class="hljs-attr">redirect</span>: location.<span class="hljs-property">pathname</span>,<br>      &#125;),<br>    &#125;);request=&#123;<span class="hljs-keyword">async</span> (params = &#123;&#125;, sort, filter) =&gt; &#123;<br>  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(sort, filter);<br>  <span class="hljs-comment">// 返回userList</span><br>  <span class="hljs-keyword">const</span> baseResponse = <span class="hljs-keyword">await</span> <span class="hljs-title function_">searchUsers</span>();<br>  <span class="hljs-keyword">return</span> &#123;<br>    <span class="hljs-attr">data</span>: baseResponse,<br>  &#125;<br>&#125;&#125;<br></code></pre></td></tr></table></figure>

<h2 id="前端封装全局请求-响应拦截器"><a href="#前端封装全局请求-响应拦截器" class="headerlink" title="前端封装全局请求/响应拦截器"></a>前端封装全局请求/响应拦截器</h2><h5 id="umi其实已经给我们封装好了request了-目录-src-umi-plugin-request-request-ts-在这里我们也可以在该目录下自定义全局请求-响应拦截器-但是一方面破坏了封装组件的完整性-不好直接修改封装好的组件-我们的想法是定义自己的requert来封装axios-再自定义全局请求-响应拦截器"><a href="#umi其实已经给我们封装好了request了-目录-src-umi-plugin-request-request-ts-在这里我们也可以在该目录下自定义全局请求-响应拦截器-但是一方面破坏了封装组件的完整性-不好直接修改封装好的组件-我们的想法是定义自己的requert来封装axios-再自定义全局请求-响应拦截器" class="headerlink" title="umi其实已经给我们封装好了request了(目录:src/.umi/plugin-request/request.ts), 在这里我们也可以在该目录下自定义全局请求/响应拦截器, 但是一方面破坏了封装组件的完整性, 不好直接修改封装好的组件. 我们的想法是定义自己的requert来封装axios, 再自定义全局请求/响应拦截器"></a>umi其实已经给我们封装好了request了(目录:src/.umi/plugin-request/request.ts), 在这里我们也可以在该目录下自定义全局请求/响应拦截器, 但是一方面破坏了封装组件的完整性, 不好直接修改封装好的组件. 我们的想法是定义自己的requert来封装axios, 再自定义全局请求/响应拦截器</h5><h5 id="新建目录-src-plugins-globalRequest-ts"><a href="#新建目录-src-plugins-globalRequest-ts" class="headerlink" title="新建目录: src/plugins/globalRequest.ts"></a>新建目录: src/plugins/globalRequest.ts</h5><h5 id="在该目录下我们封装自己的request"><a href="#在该目录下我们封装自己的request" class="headerlink" title="在该目录下我们封装自己的request"></a>在该目录下我们封装自己的request</h5><figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs typescript"><span class="hljs-keyword">import</span> &#123;extend&#125; <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;umi-request&#x27;</span>;<br><span class="hljs-keyword">import</span> &#123;message&#125; <span class="hljs-keyword">from</span> <span class="hljs-string">&quot;antd&quot;</span>;<br><span class="hljs-keyword">import</span> &#123;history&#125; <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;umi&#x27;</span>;<br><span class="hljs-keyword">import</span> &#123;stringify&#125; <span class="hljs-keyword">from</span> <span class="hljs-string">&quot;querystring&quot;</span>;<br></code></pre></td></tr></table></figure>

<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs typescript"><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * 配置request请求时的默认参数</span><br><span class="hljs-comment"> */</span><br><span class="hljs-keyword">const</span> request = <span class="hljs-title function_">extend</span>(&#123;<br>  <span class="hljs-attr">credentials</span>: <span class="hljs-string">&#x27;include&#x27;</span>, <span class="hljs-comment">// 默认请求是否带上cookie</span><br>  <span class="hljs-comment">// requestType: &#x27;form&#x27;,</span><br>&#125;);<br></code></pre></td></tr></table></figure>

<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><code class="hljs typescript"><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * 全局请求拦截器</span><br><span class="hljs-comment"> */</span><br>request.<span class="hljs-property">interceptors</span>.<span class="hljs-property">request</span>.<span class="hljs-title function_">use</span>((url, options): <span class="hljs-function"><span class="hljs-params">any</span> =&gt;</span> &#123;<br>  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`do request url = <span class="hljs-subst">$&#123;url&#125;</span>`</span>)<br>  <span class="hljs-keyword">return</span> &#123;<br>    url,<br>    <span class="hljs-attr">options</span>: &#123;<br>      ...options,<br>      <span class="hljs-attr">headers</span>: &#123;&#125;,<br>    &#125;,<br>  &#125;;<br>&#125;);<br></code></pre></td></tr></table></figure>

<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><code class="hljs typescript"><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * 全局响应拦截器</span><br><span class="hljs-comment"> */</span><br>request.<span class="hljs-property">interceptors</span>.<span class="hljs-property">response</span>.<span class="hljs-title function_">use</span>(<span class="hljs-keyword">async</span> (response, options): <span class="hljs-title class_">Promise</span>&lt;<span class="hljs-built_in">any</span>&gt; =&gt; &#123;<br>  <span class="hljs-keyword">const</span> res = <span class="hljs-keyword">await</span> response.<span class="hljs-title function_">clone</span>().<span class="hljs-title function_">json</span>();<br>  <span class="hljs-comment">// 1.返回成功状态</span><br>  <span class="hljs-keyword">if</span> (res.<span class="hljs-property">code</span> === <span class="hljs-number">0</span> &amp;&amp; res.<span class="hljs-property">data</span> &gt; <span class="hljs-number">0</span>) &#123;<br>    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(res.<span class="hljs-property">message</span>);<br>    <span class="hljs-keyword">return</span> res.<span class="hljs-property">data</span>;<br>  &#125;<br>  <span class="hljs-comment">// 2.返回失败状态</span><br>  <span class="hljs-keyword">if</span> (res.<span class="hljs-property">code</span> === <span class="hljs-number">50001</span>) &#123;<br>    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&quot;...&quot;</span>)<br>    <span class="hljs-comment">// 弹出异常信息</span><br>    message.<span class="hljs-title function_">error</span>(res.<span class="hljs-property">message</span>)<br>    <span class="hljs-comment">// 弹出异常描述</span><br>    <span class="hljs-comment">// message.error(res.description)</span><br>    <span class="hljs-comment">// 跳转至登录页</span><br>    history.<span class="hljs-title function_">replace</span>(&#123;<br>      <span class="hljs-attr">pathname</span>: <span class="hljs-string">&#x27;user/login&#x27;</span>,<br>      <span class="hljs-attr">search</span>: <span class="hljs-title function_">stringify</span>(&#123;<br>        <span class="hljs-attr">redirect</span>: location.<span class="hljs-property">pathname</span>,<br>      &#125;),<br>    &#125;);<br>    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&quot;code: &quot;</span> + res.<span class="hljs-property">code</span>)<br>  &#125;<br>  <br>  <span class="hljs-keyword">return</span> res.<span class="hljs-property">data</span>;<br><br>&#125;);<br></code></pre></td></tr></table></figure>

<h5 id="最后不要忘了-在src-services-ant-design-pro-api-ts目录下引入我们自己实现的request-覆盖掉umi封装的request"><a href="#最后不要忘了-在src-services-ant-design-pro-api-ts目录下引入我们自己实现的request-覆盖掉umi封装的request" class="headerlink" title="最后不要忘了, 在src/services/ant-design-pro/api.ts目录下引入我们自己实现的request, 覆盖掉umi封装的request"></a>最后不要忘了, 在src/services/ant-design-pro/api.ts目录下引入我们自己实现的request, 覆盖掉umi封装的request</h5><figure class="highlight clean"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs clean"><span class="hljs-keyword">import</span> request <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;@/plugins/globalRequest&#x27;</span>;<br></code></pre></td></tr></table></figure>

<h5 id="我们实现了自定义的前端全局请求-响应拦截器"><a href="#我们实现了自定义的前端全局请求-响应拦截器" class="headerlink" title="我们实现了自定义的前端全局请求/响应拦截器"></a>我们实现了自定义的前端全局请求/响应拦截器</h5><h1 id="至此-用户中心系统基本功能已经完成"><a href="#至此-用户中心系统基本功能已经完成" class="headerlink" title="至此, 用户中心系统基本功能已经完成"></a>至此, 用户中心系统基本功能已经完成</h1><h4 id="未来的持续优化"><a href="#未来的持续优化" class="headerlink" title="未来的持续优化:"></a>未来的持续优化:</h4><ul>
<li><h5 id="完善框架搭建和数据库设计的文档编写"><a href="#完善框架搭建和数据库设计的文档编写" class="headerlink" title="完善框架搭建和数据库设计的文档编写"></a>完善框架搭建和数据库设计的文档编写</h5></li>
<li><h5 id="注册的新用户会有默认的头像和昵称"><a href="#注册的新用户会有默认的头像和昵称" class="headerlink" title="注册的新用户会有默认的头像和昵称"></a>注册的新用户会有默认的头像和昵称</h5></li>
<li><h5 id="用户登录后可以更改个人信息"><a href="#用户登录后可以更改个人信息" class="headerlink" title="用户登录后可以更改个人信息"></a>用户登录后可以更改个人信息</h5></li>
<li><h5 id="完善前端全局异常处理器所处理的异常类型"><a href="#完善前端全局异常处理器所处理的异常类型" class="headerlink" title="完善前端全局异常处理器所处理的异常类型"></a>完善前端全局异常处理器所处理的异常类型</h5></li>
<li><h5 id="项目的部署和上线"><a href="#项目的部署和上线" class="headerlink" title="项目的部署和上线"></a>项目的部署和上线</h5></li>
</ul>
<h4 id="废话不多说-开启-伙伴匹配系统-的学习-（2023-04-21）"><a href="#废话不多说-开启-伙伴匹配系统-的学习-（2023-04-21）" class="headerlink" title="废话不多说, 开启 伙伴匹配系统 的学习 （2023/04/21）"></a>废话不多说, 开启 伙伴匹配系统 的学习 （2023/04/21）</h4><h2 id="项目部署"><a href="#项目部署" class="headerlink" title="项目部署"></a>项目部署</h2><h3 id="前端多环境配置"><a href="#前端多环境配置" class="headerlink" title="前端多环境配置"></a>前端多环境配置</h3><h3 id="后端多环境配置"><a href="#后端多环境配置" class="headerlink" title="后端多环境配置"></a>后端多环境配置</h3><h2 id="项目上线"><a href="#项目上线" class="headerlink" title="项目上线"></a>项目上线</h2>
                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/deng-2022/blog_2/categories/%E9%A1%B9%E7%9B%AE/" class="category-chain-item">项目</a>
  
  
    <span>></span>
    
  <a href="/deng-2022/blog_2/categories/%E9%A1%B9%E7%9B%AE/%E5%BC%80%E5%8F%91%E7%BB%8F%E9%AA%8C/" class="category-chain-item">开发经验</a>
  
  

  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/deng-2022/blog_2/tags/%E9%A1%B9%E7%9B%AE/">#项目</a>
      
        <a href="/deng-2022/blog_2/tags/%E5%BC%80%E5%8F%91%E7%BB%8F%E9%AA%8C/">#开发经验</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>Memory 用户中心中心-开发文档</div>
      <div>https://atomgit.com/deng-2022/blog_2/2023/03/17/Memory 用户中心系统-开发文档/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>Memory</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2023年3月17日</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>更新于</div>
          <div>2023年4月21日</div>
        </div>
      
      
        <div class="license-meta-item">
          <div>许可协议</div>
          <div>
            
              
              
                <a target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
                  <span class="hint--top hint--rounded" aria-label="BY - 署名">
                    <i class="iconfont icon-by"></i>
                  </span>
                </a>
              
            
          </div>
        </div>
      
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/deng-2022/blog_2/2023/03/19/MySQL%E5%AE%89%E8%A3%85%E9%85%8D%E7%BD%AE/" title="MySQL安装配置">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">MySQL安装配置</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/deng-2022/blog_2/2023/03/17/%E5%85%A5%E9%97%A8%E6%8C%87%E5%8D%97%EF%BC%9A%E6%8E%8C%E6%8F%A1Git%E7%9A%84%E5%9F%BA%E6%9C%AC%E6%93%8D%E4%BD%9C%E5%8F%8A%E8%BF%9B%E9%98%B6%E5%AD%A6%E4%B9%A0/" title="入门指南：掌握Git的基本操作及进阶学习">
                        <span class="hidden-mobile">入门指南：掌握Git的基本操作及进阶学习</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
  
  
    <article id="comments">
      

    </article>
  


          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header">
    <i class="iconfont icon-list"></i>
    <span>目录</span>
  </p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <a href="https://hexo.fluid-dev.com/docs/guide/" target="_blank" rel="nofollow noopener"> <span>Memory | 个人文档站点</span> </a>
<i class="iconfont icon-love"></i> <a href="https://hexo.fluid-dev.com/docs/guide/" target="_blank" rel="nofollow noopener"> <span>配置指南 | Hexo Fluid 用户手册</span> </a>   
    </div>
  
  
    <div class="statistics">
  
  

  
    
      <span id="leancloud-site-pv-container" style="display: none">
        总访问量 
        <span id="leancloud-site-pv"></span>
         次
      </span>
    
    
      <span id="leancloud-site-uv-container" style="display: none">
        总访客数 
        <span id="leancloud-site-uv"></span>
         人
      </span>
    
    

  
</div>

  
  
    <!-- 备案信息 ICP for China -->
    <div class="beian">
  <span>
    <a href="http://beian.miit.gov.cn/" target="_blank" rel="nofollow noopener">
      京ICP证123456号
    </a>
  </span>
  
    
      <span>
        <a
          href="http://www.beian.gov.cn/portal/registerSystemInfo?recordcode=12345678"
          rel="nofollow noopener"
          class="beian-police"
          target="_blank"
        >
          
            <span style="visibility: hidden; width: 0">|</span>
            <img src="/deng-2022/blog_2/img/police_beian.png" alt="police-icon"/>
          
          <span>京公网安备12345678号</span>
        </a>
      </span>
    
  
</div>

  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/deng-2022/blog_2/js/events.js" ></script>
<script  src="/deng-2022/blog_2/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>







  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.18.2/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init(Object.assign({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      scrollSmooth    : true,
      includeTitleTags: true,
      headingsOffset  : -boardTop,
    }, CONFIG.toc));
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }

    Fluid.events.registerRefreshCallback(function() {
      if ('tocbot' in window) {
        tocbot.refresh();
        var toc = jQuery('#toc');
        if (toc.length === 0 || !tocbot) {
          return;
        }
        if (toc.find('.toc-list-item').length > 0) {
          toc.css('visibility', 'visible');
        }
      }
    });
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.11/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));

    Fluid.events.registerRefreshCallback(function() {
      if ('anchors' in window) {
        anchors.removeAll();
        var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
        var res = [];
        for (var item of el) {
          res.push('.markdown-body > ' + item.trim());
        }
        if (CONFIG.anchorjs.placement === 'left') {
          anchors.options.class = 'anchorjs-link-left';
        }
        anchors.add(res.join(', '));
      }
    });
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/deng-2022/blog_2/js/local-search.js" ></script>

  <script defer src="/deng-2022/blog_2/js/leancloud.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/deng-2022/blog_2/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
